Javascript 如何在template helper中获取Iron路由器数据上下文
我是《流星》的新手。我在iron:router中设置数据上下文如下:Javascript 如何在template helper中获取Iron路由器数据上下文,javascript,meteor,iron-router,datacontext,Javascript,Meteor,Iron Router,Datacontext,我是《流星》的新手。我在iron:router中设置数据上下文如下: Router.route('/:index', { name:'randomText', template: 'textsRandom', data: function(){ textcol: Text.findOne({index: this.params.index}) } } 在模板textsRandom中,我想访问帮助程序中的textcol,因为我想稍后更改文本中特定单词的颜色 T
Router.route('/:index', {
name:'randomText',
template: 'textsRandom',
data: function(){
textcol: Text.findOne({index: this.params.index})
}
}
在模板textsRandom中,我想访问帮助程序中的textcol
,因为我想稍后更改文本中特定单词的颜色
Template.textRandom.helpers({
mytexts: function(){
var texts = //code here to get textcol in router.js
//get some words from texts and change their colors
return texts;
}
})
关于如何做到这一点有什么建议吗?非常感谢这应该可以做到:
// router
function(){
return {
textcol: Text.findOne({index: this.params.index})
};
}
// helper
var texts = this.textcol;
这应该做到:
// router
function(){
return {
textcol: Text.findOne({index: this.params.index})
};
}
// helper
var texts = this.textcol;
路由器正在将路由的数据上下文设置为对象。您可以通过
this
访问帮助器中的对象。由于您需要该对象的textcol
键,因此只需:
Template.textRandom.helpers({
mytexts: function(){
return this.textcol;
}
});
路由器正在将路由的数据上下文设置为对象。您可以通过
this
访问帮助器中的对象。由于您需要该对象的textcol
键,因此只需:
Template.textRandom.helpers({
mytexts: function(){
return this.textcol;
}
});