官术网_书友最值得收藏!

"this" in template helpers and template callbacks

In Meteor, this in template helpers is used differently in template callbacks such as created(), rendered(), and destroyed().

As already mentioned, templates have three callback functions that are fired in different states of the template:

  • created: This fires when the template gets initiated but is not yet in the DOM
  • rendered: This fires when the template and all its sub templates are attached to the DOM
  • destroyed: This fires when the template is removed from the DOM and before the instance of the template gets destroyed

In these callback functions, this refers to the current template instance. The instance object can access the templates DOM and comes with the following methods:

  • this.$(selectorString): This method finds all elements that match selectorString and returns a jQuery object from those elements.
  • this.findAll(selectorString): This method finds all elements that match selectorString, but returns the plain DOM elements.
  • this.find(selectorString): This method finds the first element that matches selectorString and returns a plain DOM element.
  • this.firstNode: This object contains the first element in the template.
  • this.lastNode: This object contains the last element in the template.
  • this.data: This object contains the templates data context
  • this.autorun(runFunc): A reactive Tracker.autorun() function that is stopped when the template instance is destroyed.
  • this.view: This object contains the Blaze.View instance for this template. Blaze.View are the building blocks of reactive templates.

Inside helper functions, this refers only to the current data context.

To make these different behaviors visible, we will take a look at some examples:

  • When we want to access the DOM of a template, we must do it in the rendered callback because only at this point, the template elements will be in the DOM. To see it in action, we edit our home.js file as follows:
    Template.home.rendered = function(){
      console.log('Rendered the home template');
    
     this.$('p').html('We just replaced that text!');
    };

    This will replace the first p tag that is created by the {{#markdown}} block helper, which we put there before, with the string we set. Now when we check the browser, we will see that the first <p> tag that contained our blog's introduction text has been replaced.

  • For the next example, we need to create an additional template JavaScript file for our contextExample template. To do this, we create a new file called examples.js in our templates folder and save it using the following code snippet:
    Template.contextExample.rendered = function(){
      console.log('Rendered Context Example', this.data);
    };
    
    Template.contextExample.helpers({
      logContext: function(){
        console.log('Context Log Helper', this);
      }
    });

    This will add the rendered callback as well as a helper called logContext to our contextExample template helpers. To make this helper run, we also need to add this helper to our contextExample template as follows:

    <p>{{logContext}}</p>

When we now go back to the console of our browser, we see that the data context object has been returned for all the rendered callbacks and helpers from our rendered contextTemplates template. We can also see that helpers will run before the rendered callback.

Note

In case you need access to the templates instance from inside a template helper, you can use Template.instance() to get it.

Now let's use make our template interactive using events.

主站蜘蛛池模板: 田东县| 商丘市| 万宁市| 玉田县| 高唐县| 武鸣县| 楚雄市| 阜康市| 遵义县| 衡南县| 通榆县| 于都县| 通城县| 邵阳县| 昆山市| 岳普湖县| 天津市| 义乌市| 隆化县| 兴宁市| 长泰县| 东辽县| 遵化市| 资兴市| 北票市| 南通市| 南川市| 武鸣县| 鞍山市| 区。| 遵义市| 土默特左旗| 曲沃县| 泌阳县| 西乌| 镶黄旗| 南木林县| 滕州市| 定西市| 东阳市| 灵台县|