2016-06-13 23 views
1

我是Meteor的新手。我设置在铁的数据上下文:路由器如下:如何在模板助手中获得Iron路由器数据上下文

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

有关如何做到这一点的任何建议?非常感谢

回答

0

你的路由器设置路由的数据上下文的对象。您可以访问助手中的对象this。既然您想要该对象的textcol键,那么只需:

Template.textRandom.helpers({ 
    mytexts: function(){ 
     return this.textcol; 
    } 
}); 
0

这应做到:

// router 
function(){ 
    return { 
     textcol: Text.findOne({index: this.params.index}) 
    }; 
} 

// helper 
var texts = this.textcol; 
相关问题