流星模板数据上下文在template.ad中不可用,刷新后创建 [英] Meteor template data context not available in template.created upon refresh
问题描述
我在模板的created
函数中使用模板数据上下文.
I use the template data context inside a template's created
function.
Template.temp.created = function() { console.log('this.data'); };
当我正常进入页面时-即单击页面的链接-我看到控制台记录了正确的数据对象.当我点击页面上的刷新按钮时,this.data
是null
.
When I go to the page normally--i.e. click the link to the page--I see the console log the correct data object. When I hit the refresh button on the page, this.data
is null
.
为什么?
此外,我正在使用Iron-Router设置数据上下文:
Also, I am using iron-router to set the data context:
...
this.route('temp', {
...
data: function() { return MyCollection.findOne(someId); },
...
}
...
推荐答案
如果要等到数据到来,请使用waitOn
.
If you want to wait until data come then use waitOn
.
this.route('temp', {
waitOn:function(){
return this.subscribe("nameOfPublishFunction");
},
data: function() { return MyCollection.findOne(someId); },
...
}
记住要激活加载挂钩(感谢@Peppe L-G):
Remember to activate loading hook (thanks @Peppe L-G):
Router.onBeforeAction("loading");
此处,您可以找到带有iron:router软件包的示例流星应用程序,它显示了如何打开和关闭加载挂钩(Router.onBeforeAction("loading")
)如何将数据的可用性更改为created
和rendered
方法.
Here you can find sample meteor app with iron:router package which shows how turning loading hook on and off ( Router.onBeforeAction("loading")
) changes availability of data to created
and rendered
methods.
这篇关于流星模板数据上下文在template.ad中不可用,刷新后创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!