javascript - making ajax request in meteor helpers -
how can wait until ajax request finishes when returning data meteor helpers method.
for example,
template.item.helpers({ itemname:function () { var user = meteor.user(); $.when(reallylongajaxrequest()).done(function (a1) { //tried using jquery when return "item name should have because waited"; }); return " doesnt wait @ all"; } });
i have reallylongajaxrequest()
running , finish before continuing on itemname helper. log statement console shows undefined that's because ajax request hasn't finished. tried using jquery when no luck. ideas
edit:
i should mention inside helper function reason. need item 'id' being rendered can run ajax request paramater. using reactive sessions perfect don't know of way rendering items outside of helpers method definition?
an unnamed collection 1 null
passed name. in-memory data structure, not saved database. (http://docs.meteor.com/#meteor_collection)
ok, given meteor collection called "items" , wanting ajax request each item based on item _id, , being able reference ajax result in template, i'd do:
(roughly)
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 html you'll need pass in id helper:
{{itemname _id}}
is there no way timeout few seconds when defining helper ajax request finishes without returning.
no, reactive programming things happen immediately, update when have new stuff.
Comments
Post a Comment