Meteor 从渲染函数中的iron路由器访问数据
我试图在javascript函数中访问从iron router传递的数据 路由器.jsMeteor 从渲染函数中的iron路由器访问数据,meteor,iron-router,Meteor,Iron Router,我试图在javascript函数中访问从iron router传递的数据 路由器.js this.route('editOrganization', { path: '/editOrganization', waitOn: function() { return [ Meteor.subscribe('organization', this.userId) ]; }, data: function() { r
this.route('editOrganization', {
path: '/editOrganization',
waitOn: function() {
return [
Meteor.subscribe('organization', this.userId)
];
},
data: function() {
return Organizations.findOne();
}
});
现在,如果我想访问html(editCompany.html)中的organization属性,我可以执行以下操作
{{name}}
但是如何访问js文件中的相同属性呢
Template.editOrganization.rendered = function() {
//how do I access name?
}
更新:
因此,如果我单击一个链接来编辑组织,我可以通过
this.data.name
但是,如果我重新加载页面(相同的url),它会抛出一个错误,说明数据为空。可以通过呈现的函数上下文访问它
Template.editOrganization.rendered = function() {
var name = this.data && this.data.name;
};
这对许多人来说都很困惑,但您需要将路由器配置为实际等待使用waitOn返回的订阅
Router.onBeforeAction('loading')
您可以在此处阅读作者的解释:
应该是Template.editOrganization.rendered吗?是的,很抱歉,只是很快就写出来了,但代码中没有问题(已更改),所以如果单击链接编辑组织,我会得到值,但如果重新加载,它抛出一个错误,表示数据是无效的null@Saad更新为包括全局
onBeforeAction
,并检查此数据是否已定义。