Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在template helper中获取Iron路由器数据上下文_Javascript_Meteor_Iron Router_Datacontext - Fatal编程技术网

Javascript 如何在template helper中获取Iron路由器数据上下文

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

我是《流星》的新手。我在iron:router中设置数据上下文如下:

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