dalgard / meteor-viewmodel

Minimalist VM for Meteor
24 stars 2 forks source link

this.getData() - is non-reactive #24

Closed comerc closed 8 years ago

comerc commented 8 years ago

In API help:

// Reactively get the data context of the current template instance
this.getData();

In source-code:

  // Reactively get the template's data context
  getData() {
    return this.templateInstance().data;
  }

According to Meteor's documentation, about template.data:

This property provides access to the data context at the top level of the template. It is updated each time the template is re-rendered. Access is read-only and non-reactive.

dalgard commented 8 years ago

I'm actually a little unsure myself about how the call to templateInstance() is reactive – when, exactly, is it invalidated? Anyway, I'm simply passing this reactivity on to the user of ViewModel.

comerc commented 8 years ago

We may use Template.currentData():

Establishes a reactive dependency on the result.

Or not?

UPD:

Uncaught Error: There is no current view

OK, we may use Template.currentData() in this cases with fields of Template.instance().viewmodel.

  • Inside an onCreated, onRendered, or onDestroyed callback, returns the data context of the template.
  • Inside an event handler, returns the data context of the template on which this event handler was defined.
  • Inside a helper, returns the data context of the DOM node where the helper was used.