ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

Problem with helpers using references being called from template (Blaze) #288

Closed arggh closed 7 years ago

arggh commented 7 years ago

When a helper uses references as described in the Controls section of docs, this gets thrown:

Exception in template helper: TypeError: Cannot read property 'enabled' of undefined`

If I change the code so that the helper only gets called if I click a button (for example), it works fine.

Minimal reproduction:

<template name="parentTemplate">
  {{#if isEnabled}}
    ENABLED
  {{/if}}

  {{> childTemplate ref='childtemp' }}
</template>

<template name="childTemplate">
  <button {{b "toggle: enabled"}} type="button">toggle</button>
</template>
Template.parentTemplate.viewmodel({
  isEnabled() {
    return this.childtemp.enabled();
  }
});

Template.childTemplate.viewmodel({
  enabled: false
});

Now, if I exchange that {{#if isEnabled}} into a div <div {{b "if: isEnabled">... it works.

Tested with Meteor 1.4.2.3.

ManuelDeLeon commented 7 years ago

There's no fix for it since the parent #if is invoked before the child is created. You'd have to check for the existence of the reference or use the if binding (which happens after the child is created).

On May 28, 2017 7:08 AM, "arggh" notifications@github.com wrote:

When a helper uses references as described in the Controls section of docs https://viewmodel.org/docs/misc#controls, this gets thrown:

Exception in template helper: TypeError: Cannot read property 'enabled' of undefined`

If I change the code so that the helper only gets called if I click a button (for example), it works fine.

Minimal reproduction:

Template.parentTemplate.viewmodel({ isEnabled() { return this.childtemp.enabled(); } }); Template.childTemplate.viewmodel({ enabled: false });

Now, if I exchange that {{#if isEnabled}} into a div <div {{b "if: isEnabled">... it works.

Tested with Meteor 1.4.2.3.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/288, or mute the thread https://github.com/notifications/unsubscribe-auth/AED31ky5BvBTNUVGbz3FA9TgSREw5Y92ks5r-XG8gaJpZM4NopH2 .

arggh commented 7 years ago

Thanks for the clarification 👍