[英]How do I return async value to a template in Meteor
I have a helper function in my Meteor app as follows 我在Meteor应用中有一个辅助功能,如下所示
Template.items.helpers({
data: function () {
return Template.instance().myAsyncValue.get();
}
});
Template.items.created = function (){
console.log('template created')
var self = this;
self.myAsyncValue = new ReactiveVar("Waiting for response from server...");
Meteor.call('get_itemes', function (err, asyncValue) {
if (err)
console.log(err);
else
self.myAsyncValue.set(asyncValue);
console.log('asyncValue'+asyncValue);
});
}
Template.items.rendered = function() {
console.log('template rendered')
};
I am basically trying to send that data over to template for display. 我基本上是试图将数据发送到模板进行显示。 Inside my get_items function, I am making an asynchronous call to Parse, to retrieve some objects.
在我的get_items函数中,我对Parse进行了异步调用,以检索一些对象。 I've taken a look at this post but I'm still unclear as to what the preferred method to return async data to a template in meteor is.
我看了这篇文章,但对于将异步数据返回到流星中的模板的首选方法仍然不清楚。
Here is my get_items function 这是我的get_items函数
get_items: function() {
var Item = Parse.Object.extend("Item");
var query = new Parse.Query(Item);
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length + " scores.");
console.log(results);
return results
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
return error;
}
});
}
even though I am logging success in the async function that is calling parse, it does not seem to be getting returned to the reactive variable, Am I implementing this wrong? 即使我在调用parse的异步函数中记录了成功,它似乎也没有返回到反应变量,我是否实现了这个错误?
You can save it to a local null collection and use {{#each}}{{/each}}
. 您可以将其保存到本地null集合并使用
{{#each}}{{/each}}
。 For example: 例如:
Items = new Mongo.Collection(null);
Template.items.onCreated(function () {
Meteor.call('myMethod', function (err, results) {
if (!err) {
results.forEach(function (item) {
Items.insert(item);
})
}
});
});
Template.items.helpers({
items: function () {
return Items.find();
}
});
Then use an each. 然后使用每个。
{{#each items}}
{{ value }}
{{/each}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.