javascript - Accessing custom template property in helper -
i know why need use template.instance()
rather this
in helpers access property attached template.
here code question arose.
template.mytemplate.oncreated(function () { this.myproperty = new reactivevar(1); }); template.mytemplate.helpers({ myproperty: function () { return template.instance().myproperty.get(); // works return this.myproperty.get(); // not work. (this.myproperty undefined) } });
i thought this
inside helper reference template instance. why not second 1 work?
in body of callback under template.oncreated, template.onrendered , template.ondestroyed, this
template instance object.
however, within helpers, this
data context of dom node helper used not template instance. example,
html
{{> mytemplate name='max'}} template(name='mytemplate') ul {{#each users}} li {{getavatar}} {{/each}}
js
template.mytemplate.oncreated(function(){ console.log(this); // template.instance() }) template.mytemplate.helpers { users: function() { console.log(this); // {name: 'max'} }, getavatar: function() { console.log(this); // {_id: ..., username: ..., profile: ..., ...} } }
the above know far, if there mistakes, please point out, thanks.
Comments
Post a Comment