Access to the helper template in the Meteor event handler

In Meteor, I send two objects from my db to the template:

Template.myTemplate.helpers({ helper1: function() { var object1 = this; // data context set in iron:router...path is context dependent // modify some values in object1 return this; }, helper2: function() { return Collection2.find({_id: this.object2_id}); } }); 

This template also has an event handler for modifying the two above objects. I am trying to access helper1 and helper2 from above, but if I call the template data context, I get access to an unmodified version of object1. How to access the helpers mentioned above?

 Template.myTemplate.events({ 'submit form': function(event) { event.preventDefault(); // Access helper2 object and attributes here instead of calling Collection2.find() again } }); 
+5
source share
2 answers

Helpers are just functions and, thus, can be transferred and assigned to other variables as you like, so you can define a function and then assign it the helper2 key using the template helpers and call the source link from the event handler on it.

 var helperFunction = function() { return Collection2.find({_id: this.object2_id}); }; Template.myTemplate.helpers({ helper1: function() { var object1 = this; // data context set in iron:router...path is context dependent // modify some values in object1 return this; }, helper2: helperFunction }); Template.myTemplate.events({ 'submit form': function(event) { event.preventDefault(); var cursor = helperFunction(); } }); 
+4
source

If you can change helpers from events, then any part of the Meteor app can do that contrary to Blaze's design philosophy!

Blaze is designed for a unidirectional data binding sampling system. What you ask for can be achieved using Angular (used alone or side by side with Blaze, see THIS ), which by its nature is a 2-way data binding system.

You can also check React, which is also a binding in two ways.

0
source

Source: https://habr.com/ru/post/1211153/


All Articles