Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do Template.currentData() and template.data differ in value?

Tags:

meteor

I know one is a reactive source, while the other is not. But I thought they would always give the same value.

Then I found the following code in Telescope's 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

So it seems these two values can sometimes differ. When?

like image 842
Gary Chang Avatar asked Sep 10 '15 15:09

Gary Chang


People also ask

What is the difference between default template and data template?

Default template defines a look and feel, basically a style of control. That's why by default, Button or TextBox shapes are ectangular because it is defined in its default template. Now we can update that template and add our own implementation. Data Template: Customize the functionality.

What is default data template in Salesforce?

Default data templates Configuration data templates are predefined lists of entities for each module area that can be used in a data project. You can create, view, and modify these templates by using the Template page in the Data management workspace.

How to use datatemplate template in WPF?

But you can do the same in the code behind. We need to use ContentPresenter when we want to use DataTemplate. Then you must specify its content, and the source from where our inner control is supposed to fetch its data. Then bind each property as you wish. I hope this article has helped you to understand Templates in WPF.

What is the difference between a datatemplate and a controltemplate?

A DataTemplate, therefore, is used to provide visual structure for underlying data, while a ControlTemplate has nothing to do with underlying data and simply provides visual layout for the control itself.


1 Answers

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.

Since we know that the current data context is reactive, hence can change without the template being re-rendered (which is what makes reactivity look all nice and smooth on Blaze), this if statement is written to check if the "real" current terms (which are stored in the reactive Template.currentData()) have changed compared to the "previous" terms we had the last time the current template was rendered. (stored in the non-reactive template.data)

To wrap it up, what this autorun does is:

  1. Anytime the current data context changes...
  2. Fetch the terms from said data context
  3. Compare these terms to the ones stored in template.data when the template was rendered
  4. If they differ, that means the terms have changed (duh): reset the post limit.
like image 119
SylvainB Avatar answered Nov 16 '22 03:11

SylvainB