2016-06-13 26 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; 
相關問題