Creating ajax request in meteor assistants

How can I wait for the ajax request to complete when returning data to the meteor assistants method.

For instance,

Template.item.helpers({ itemName:function () { var user = Meteor.user(); $.when(reallyLongAjaxRequest()).done(function (a1) { //tried using jquery when return "Item Name should have this because it waited"; }); return " Doesnt wait at all"; } }); 

reallyLongAjaxRequest() works for me, and I would like it to finish before continuing with my itemName helper. The log statement on the console always shows undefined, but that is because the ajax request is not finished yet. I tried using jquery when you were out of luck. Any ideas

Edit:

I should mention that I am inside a helper function for some reason. I need the "id" element to display so that I can run an ajax request with this parameter. Using reactive sessions would be ideal, but I don’t know how to get elements currently rendering outside of the definition of helper method?

+6
source share
3 answers

An unnamed collection is one where null is passed for the name. This is a data structure in memory not stored in the database. ( http://docs.meteor.com/#meteor_collection )

OK, given the Meteor collection called "items", and you want to execute an ajax request for each item based on the _id element, and then being able to reference the ajax result in the template, this is what I would do:

(about)

 var Items = new Meteor.Collection('items'); var Results = new Meteor.Collection(null); Items.find().observeChanges({ added: function (id) { $.get(url, {id: id}, function (data) { if (Results.findOne(id)) Results.update(id, {$set: {result: data}}); else Results.insert({_id: id, result: data}); }); } }); Template.item.itemName = function (id) { var doc = Results.findOne(id); if (doc) return doc.result; else return ""; }; 

inside your html you need to pass the id to the helper:

 {{itemName _id}} 

Is it not possible to simply skip the pause for a few seconds when defining an helper so that my ajax request completes without returning immediately.

No, with reactive programming everything happens right away, but you update when you have new things.

+5
source

Make your ajax request separately, and when it completes, save the result in a session variable. Then return the value for the Session variable. Rough...

 $.get(url, function (data) { Session.set('result', data); }); Template.item.itemName = function () { return Session.get('result'); }; 

A session is a reactive data source, so your template will automatically update when the result of an ajax call arrives. (Naturally, you can choose to call the Session variable, whatever, I just used the "result" as an example).

+1
source

This works and is tested in MeteorJS> 1.3.x

Add the http package from the meteor add http console

An example of a POST request with sending data items to the server and with custom headers.

 HTTP.call('POST', tokenUri, { data: { "type": 'authorization_code', //"client_id": clientId, "code": code, "redirect_uri" : redirectUri, }, headers: { "Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials" : "true", "Access-Control-Allow-Methods" : "GET,HEAD,OPTIONS,POST,PUT", "Access-Control-Allow-Headers" : "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers", } },function(error, response) { if ( error ) { console.log( error ); } else { console.log( response ); } }); 
-1
source

All Articles