流星模板数据上下文在template.ad中不可用,刷新后创建 [英] Meteor template data context not available in template.created upon refresh

查看:83
本文介绍了流星模板数据上下文在template.ad中不可用,刷新后创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板的created函数中使用模板数据上下文.

I use the template data context inside a template's created function.

Template.temp.created = function() { console.log('this.data'); };

当我正常进入页面时-即单击页面的链接-我看到控制台记录了正确的数据对象.当我点击页面上的刷新按钮时,this.datanull.

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");

IronRouter docs #waitOn

此处,您可以找到带有iron:router软件包的示例流星应用程序,它显示了如何打开和关闭加载挂钩(Router.onBeforeAction("loading"))如何将数据的可用性更改为createdrendered方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆