javascript - Why doesn't this function run when the reactive variable changes value? -
i'm new meteor , i'm trying hang of whole reactivity thing.
there isn't specifc reason why want function re-run, in fact, not re-running desired behavior use case. want know why happening can better understand concepts.
if add function property on template instance, this:
template.services.oncreated( function() { this.templates = [ "web_design", "painting", "gardening" ]; this.current_index = new reactivevar(0); this.determineslidedirection = function() { console.log(this.current_index.get()); }; });
and update reactive var in response event.
template.services.events({ 'click .nav-slider .slider-item': function(event, template) { var new_selection = event.currenttarget; template.current_index.set($(new_selection).index()); } });
the function not re-run upon invocation of set()
call.
however, if have helper utilizes variable, re-run.
template.services.helpers({ currenttemplate: function() { var self = template.instance(); return self.templates[self.current_index.get()]; } });
why this?
reactive data sources cause functions automatically re-run. these functions are:
tracker.autorun
template.mytemplate.helpers({})
blaze.render
,blaze.renderwithdata
in code above want use tracker.autorun
template.services.oncreated( function() { this.templates = [ "web_design", "painting", "gardening" ]; this.current_index = new reactivevar(0); tracker.autorun(function(){ // actually, might not work because context of // 'this' might changed when inside of tracker. this.determineslidedirection = function() { console.log(this.current_index.get()); }; }); });
Comments
Post a Comment