Vartan
Vartan

Reputation: 677

"this" value in template helper returns Window

I have the following helper

Template.meetingRow.helpers({
  isOwner: () => {
    console.log(this);
    return Meteor.userId() === this.owner;
  }
});

which will log the Window object to the console.

The this object returns the correct object in Template.meetingRow.events, and uses the correct values in the template, but isn't correct in the helper. Am I misunderstanding something?

Here's how it's being instantiated in its parent template:

<ul class="list-group">
  {{#each meetings}}
    {{ > meetingRow }}
  {{/each}}
</ul>

Upvotes: 2

Views: 131

Answers (1)

richsilv
richsilv

Reputation: 8013

It's because in ES2015, arrow functions inherit the context of the surrounding function, rather than being given their own. What this means is that Meteor can't bind a context to your helper function if it's defined this way.

Solution (in ES2015 style):

Template.meetingRow.helpers({
  isOwner() function {
    console.log(this);
    return Meteor.userId() === this.owner;
  }
});

More here under "Lexical this".

Upvotes: 4

Related Questions