【问题标题】:How can I get Iron router data context in template helper如何在模板助手中获取 Iron 路由器数据上下文
【发布时间】:2016-06-14 13:36:48
【问题描述】:

我是 Meteor 的新手。我在 iron:router 中设置数据上下文如下:

Router.route('/:index', {
   name:'randomText',
   template: 'textsRandom',
   data: function(){
      textcol: Text.findOne({index: this.params.index})
   }
}

而在模板 textsRandom 中,我想在 helper 中访问 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;
   }
})

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

【问题讨论】:

    标签: javascript meteor iron-router datacontext


    【解决方案1】:

    您的路由器正在将路由的数据上下文设置为一个对象。您可以使用this 访问帮助程序中的对象。既然您想要该对象的 textcol 键,那么只需:

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

    【讨论】:

      【解决方案2】:

      应该这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-07
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        • 1970-01-01
        • 2015-10-16
        • 2017-02-01
        • 1970-01-01
        相关资源
        最近更新 更多