[solved] Adding events handlers to template dynamically in helpers


#1

Hi everyone,
for some reason, i need to add an event handler to a template through an helper. Is it even possible ?
To complicate things, the event handler “key” should be dynamically generated.
I have tried this:

    Template.foo.helpers({
      setter : function(id){
        var instance = Template.instance();
        var key = "click " + "#" + id;
        var eventsMap = {};
        eventsMap[key] = function(){
          //doStuff....
        }
        instance.events(eventsMap);
      }
    });

The problem is that it doesn’t work.
The instance variable doesn’t have any “events” method.
Is there a solution ?
Thanks in advance.


#2

Without going too far into the code, my first observation is that you should have

var instance = Template.instance();

(i.e. “=” not “:”)


#3

Yeah, that’s a typo, the example i wrote here is not the actual code.


#4

The Blaze.Template object has the events method. You should call Template.foo.events(eventsMap) instead of Template.instance().events(eventsMap).


#5

Thank you Gwendal, i was trying something too complicated.