xguox
xguox

Reputation: 751

same helper for different template

as the official meteor wiki about handlebars said:

The only way to make a helper visible to multiple templates is to assign the same function to each one, or to declare a global helper.

I am using the telescope to build an app and the problem is the value of "member since" in user-profile is missing like the follow image.
enter image description here
as the source code in /client/views/users/user_profile.js Template.user_profile.createdAtFormatted = Template.user_item.createdAtFormatted; does not work well.

and here is the helper in /client/views/users/user_item.js Template.user_item

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

,when I change the code in /client/views/users/user_profile.js like this:

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

or make a Global helpers like this

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

then, everything is ok like this
enter image description here

all I wonder is why the assignment Template.user_profile.createdAtFormatted = Template.user_item.createdAtFormatted; did not work as the way I or the author wish?

Upvotes: 1

Views: 1597

Answers (1)

Tarang
Tarang

Reputation: 75945

This is probably because of the helper & a slight inconsistency in Meteor. I think the helpers were added later in Meteor.

Basically there are two ways to define helpers in meteor. One is:

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

and the below should do the same thing:

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

But the latter using helpers doesnt create a Template.user_item.createdAtFormatted definition so the above comes out as undefined. This is most likely a bug in meteor and should be fixed. I've opened a issue: https://github.com/meteor/meteor/issues/886

Upvotes: 3

Related Questions