【问题标题】:same helper for different template不同模板的相同助手
【发布时间】:2013-03-27 23:18:14
【问题描述】:

正如official meteor wiki about handlebars所说:

让一个帮助器对多个模板可见的唯一方法是为每个模板分配相同的函数,或者声明一个全局帮助器。

我正在使用telescope 构建应用程序,问题是用户配置文件中的“member since”的值缺失,如下图所示。

作为 /client/views/users/user_profile.js 中的源代码 Template.user_profile.createdAtFormatted = Template.user_item.createdAtFormatted; 效果不好。

这里是 /client/views/users/user_item.js Template.user_item 中的助手

Template.user_item.helpers({
 createdAtFormatted: function(){
   return this.createdAt ? moment(this.createdAt).fromNow() : '–';
})

,当我像这样更改 /client/views/users/user_profile.js 中的代码时:

Template.user_profile.createdAtFormatted = function() {
     return this.createdAt ? moment(this.createdAt).fromNow() : '–';
}

或者做一个像这样的全局助手

Handlebars.registerHelper("createdAtFormatted", function(){
     return this.createdAt ? moment(this.createdAt).fromNow() : '–';
});

那么,就这样就好了

我只想知道为什么Template.user_profile.createdAtFormatted = Template.user_item.createdAtFormatted; 的作业没有按我或作者希望的方式工作?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    这可能是因为 helper 和 Meteor 中的轻微不一致。我认为这些助手是后来在 Meteor 中添加的。

    在meteor中定义助手的方法基本上有两种。一种是:

    Template.user_item.createdAtFormatted = function() {...}
    

    下面应该做同样的事情:

    Template.user_item.helpers({
        'createdAtFormatted':function() {
        ...
        }
    });
    

    但后者使用helpers 不会创建Template.user_item.createdAtFormatted 定义,因此上述结果为undefined。这很可能是流星中的一个错误,应该修复。我打开了一个问题:https://github.com/meteor/meteor/issues/886

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2015-01-28
      • 2014-02-03
      • 2013-07-17
      相关资源
      最近更新 更多