meteor icon indicating copy to clipboard operation
meteor copied to clipboard

Second handler for a template's event replaces the first one

Open VasilySizov opened this issue 13 years ago • 5 comments

Template.commandLine.events
    'keyup .commandLine_input' : (evt) ->
        console.log('a')

Template.commandLine.events
    'keyup .commandLine_input' : (evt) ->
        console.log('b')

The code above returns only "b" in the console. But it would be better if it appends new event handlers for the template instead of replacing.

Current behaviour causes a lot of problems when you assign handlers for a template's event from various places.

For example, you have an "element A", "element B" and "element C" within separate js files (a.js, b.js, c.js). You would like do something within the "element B" and "element C" after click on the "element A". So you attach a click event handler for element A within b.js and c.js. But the result is that the event handler within b.js doesn't work. It's replaced with c.js, because it loads later.

Temporary workaround for the problem found is given below.

Template.commandLine.events
    'keyup .commandLine_input' : (evt) ->
        console.log('a')

Template.commandLine.events
    'keyup .some_fake_identifier, keyup .commandLine_input' : (evt) ->
        console.log('b')

The code prints "a", "b" in the console.

Thanks.

VasilySizov avatar Jan 30 '13 12:01 VasilySizov

Nice catch! I think this problem occurs because all of the events are internally accumulated as keys in an object, so if you have two events with exactly the same spec (down to whitespace) then the later one will overwrite the earlier one. The place where this happens is probably packages/templating/deftemplate.js around line 82. So rather than adding .some_fake_identifier, just adding a space on the end of the string would probably work as a workaround.

This would be a good starter project for someone who was interested in contributing to Meteor and learning a little bit about the internals. Here's how you would do it:

  • In Spark.attachEvents, allow the values in eventMap to be either functions or arrays of functions. (Right now they can only be functions.) This will be easy because handlerMap is already kept as a list and allows duplicate selectors. Update the Spark documentation in the wiki to reflect this.
  • In deftemplate.js, where _tmpl_data.events is constructed (around line 82), have the values be arrays of functions, so that if a duplicate event specification is passed in it can just be appended to the array.

I think if you do that then everything will work. Anyone interested in giving it a shot?

gschmidt avatar Feb 04 '13 23:02 gschmidt

It'd be happy to give this a shot. It might take a day or two for me to get to it, so someone can steal it from me if they want :>.

Based off of devel branch?

jagill avatar Feb 05 '13 19:02 jagill

Hi just some input:

The problem relates to the use of underscore and the event object : packages/templating/deftemplate.js ln 79-83:

    events: function (eventMap) {
      var events =
            (this._tmpl_data.events = (this._tmpl_data.events || {}));
      _.extend(events, eventMap);
    }

Future solution?: Maybe convert events to an array eg.:

[{ spec: 'event selector', callback: function() {} },{ spec: 'event selector', callback: function() {} }, ... ]

*This will deprecate the old way of setting event objects, now done by overriding the events function:

  Template.foo.events = {
   'event selector': function() {}
  };

except if one follows the array pattern

Code changes: packages/templating/deftemplate.js ln 79-83:

    events: function (eventMap) {
      var events =
            (this._tmpl_data.events = (this._tmpl_data.events || []));
      _.each(eventMap, function(callback, key) {
        events.push({ spec: key, callback: callback});
      });
    }

Spark.attachEvents() expects events to be an object uses _.each to iterate over events so this will have to change a bit: packages/spark/spark.js ln 739:

  _.each(eventMap, function(eventHandle, key) {  //changed
    var callback = eventHandle.callback;  //added
    var spec = eventHandle.spec;            //added
    var clauses = spec.split(/,\s+/);

None of the code is tested

raix avatar Feb 07 '13 07:02 raix

Hey @jagill are you working on this?

AVGP avatar Feb 12 '13 05:02 AVGP

@AVGP: I am! Sorry for the delay -- the new year was a bit more exciting than expected, involving llamas and fermented mare's milk.

@gschmidt: I have the change in mad-eye/meteor#eventhandlers , but I wanted to check with you about testing. Currently I have it tested using the mocha-web smart package at mad-eye/meteor-eventhandlers, but felt I should have some more 'native' tests. However, the tests in the spark and templating packages seemed a bit thorny, and I wanted to check with you about your testing procedure before diving into them!

jagill avatar Feb 12 '13 08:02 jagill