meteorjs iron-router waitOn并用作渲染的数据 [英] meteorjs iron-router waitOn and using as data on rendered
问题描述
我尝试在Template.rendered
函数中获取返回的数据.
I try to get the returned data in my Template.rendered
function.
当前代码为:
this.route('editCat', {
layoutTemplate : 'layoutCol2Left',
template : 'modCategoriesEdit',
path : '/mod/categories/edit/:_id',
yieldTemplates : _.extend(defaultYieldTemplates, {
'navigationBackend' : {to : 'contentLeft'}
}),
waitOn : function () {
return Meteor.subscribe('oneCat', this.params._id);
},
data : function () {
return Categories.findOne({_id : this.params._id});
}
});
在此块中,我等待对Collection Document
的订阅,然后将文档作为数据返回.
In this block i wait on the subscribtion of the Collection Document
and return the Document as data.
现在我可以像这样在我的模板中使用返回的文档了:
Now i can use the returned Document in my Template like this:
<template name="modCategoriesEdit">
<h1>Edit {{name}}</h1>
</template>
我的问题是我必须在渲染函数中使用返回的数据,如下所示:
My problem is that i have to use the returned data in my rendered function like this:
Template.modCategoriesEdit.rendered = function () {
console.log(this.data);
}
但这将返回"null".
But this returns "null".
所以我的问题是: 如何在渲染函数中访问返回的数据?
So my question is: How is it possible to get access to the returned data in the rendered function ?
推荐答案
解决方案:
只需将以下内容添加到您的铁路由器route()方法中即可.
Just add the following to your iron-router route() method.
action : function () {
if (this.ready()) {
this.render();
}
}
比正确加载所有模板后,模板将呈现.
Than the Template will rendered after all is loaded correctly.
这篇关于meteorjs iron-router waitOn并用作渲染的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!