When do the values ​​of Template.currentData () and template.data differ in value?

I know that one of them is a reactive source, and the other is not. But I thought that they would always give the same meaning.

Then I found the following code in the telescope source:

var newTerms = Template.currentData().terms; // ⚑ reactive ⚑ if (!_.isEqual(newTerms, instance.data.terms)) { instance.postsLimit.set(instance.data.terms.limit || Settings.get('postsPerPage', 10)); } 

link: https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-posts/lib/client/templates/posts_list/posts_list_controller.js#L33

Thus, it seems that these two meanings can sometimes be different. When?

+7
meteor
source share
2 answers

According to the Meteor documentation, template.data :

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

Since we know that the current data context is reactive , therefore, it can change without re-rendering the template (this is what makes reactivity look beautiful and smooth Blaze), this if written to check if the "real" current terms have changed (which are stored in reactive Template.currentData() ) compared to the β€œprevious” terms that we had the last time the current template was made . (stored in non-reactive template.data )

To complete this, what does this autostart do:

  • At any time, the current data context changes ...
  • Get conditions from the specified data context
  • Compare these terms with those that were saved in template.data when rendering the template
  • If they differ from each other, it means that the terms have changed (duh): reset post-limit.
+8
source share

In post_list_controller.js in the onCreated template there is a section with:

 // initialize the reactive variables instance.terms = new ReactiveVar(instance.data.terms); 

This reactive variable gets the meaning of the term through:

 {{> Template.dynamic template=template data=data}} // see posts_list_controller.html 

Whenever you pass this template twice with a different dataset ( David Weldon - scoped-reactivity ), this reactive variable will be set and will cause autorun to start.

 // this part will cause the autorun to run var terms = Template.currentData().terms; // ⚑ reactive ⚑ 
+2
source share

All Articles