meteor - How do I access the data context and the template instance in each case (event, helper, hook)? -


my brain hurting because of inconsistency. please have @ code below , correct/complete it:

template.example.events({   'click #example': function(event, template) {     instance = template; // or = template.instance();     instance_reactive_data_context = template.currentdata(); // or = template.currentdata();     instance_nonreactive_data_context = ???     event_data_context = event.currenttarget; });  template.example.helpers({   example: function() {     instance = template.instance();     instance_reactive_data_context = this; // or = template.currentdata();     instance_nonreactive_data_context = ???   } });  template.example.oncreated(function () {   instance = this;   instance_reactive_data_context = this.currentdata();   instance_nonreactive_data_context = this.data; }); 

here's answer, shows bit more. includes creating , accessing reactive-var or reactive-dictionaries attached template. extremely important understand meteor developers:

template.example.oncreated(function () {   instance = this; // or = template.instance();   // instance_reactive_data_context = no point in having reactive data context since function executed once   instance_nonreactive_data_context = this.data;   // in order attach reactive variable template:   let varinitialvalue = ...   instance.reactive_variable = new reactivevar(varinitialvalue);   // , let's attach 2 reactive dictionaries template:    let dictinitialvalue_1 = { ... }   let dictinitialvalue_2 = [ ... ]   instance.reactive_dictionaries = new reactivedict();   instance.reactive_dictionaries.set('attacheddict_1', dictinitialvalue_1);   instance.reactive_dictionaries.set('attacheddict_2', dictinitialvalue_2); });  template.example.events({   'click #example': function(event, template) {     instance = template; // or = template.instance();     instance_reactive_data_context = template.currentdata();     instance_nonreactive_data_context = template.data;     event_data_context = event.currenttarget;     // access or modify reactive-var attached template:     console.log(template.reactive_variable.get());     template.reactive_variable.set('new value');     // access or modify 1 of reactive-dictionaries attached template:     console.log(template.reactive_dictionaries.get('attacheddict_2'));     template.reactive_dictionaries.set('attacheddict_2', { newkey: 'new value', somearray: ['a', 'b'] }); });  template.example.helpers({   example: function() {     instance = template.instance();     instance_reactive_data_context = this; // or = template.currentdata();     // instance_nonreactive_data_context = can't accessed non-reactive source. when you'll need this, because helper running many times, [meteor-computed-field][1] package     // access or modify reactive-var attached template:     console.log(template.instance().reactive_variable.get());     template.instance().reactive_variable.set('new value');     // access or modify 1 of reactive-dictionaries attached template:     console.log(template.instance().reactive_dictionaries.get('attacheddict_2'));     template.instance().reactive_dictionaries.set('attacheddict_2', 'new value here');     // since declared instance on first line, you'd use everywhere "instance." instead of "template.instance()."   } }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -