Why does Meteor have strong event handler syntax?

Something that bothered me about the Meteor is how events are tied to patterns. It’s not so much as not to use jQuery, but I just can’t stop my suspicion that something is wrong with the way they are written.

Here is an example:

Template.input.events = {
    'mousedown input#message' : function(event){
        console.log('the mouse was pressed');
    },

    'touchstart input#message' : function(event){
        console.log('a finger was pressed');
    },
}

Can someone explain to me why the event name and query selector will be combined in one line, for example 'mousedown input#message'? I just don't understand why anyone would like to do something that worked this way.

If it were me, I would also have laid out the events under each selector. Note: this code does not work, this is exactly how I think it should look.

Template.input.events = {
    'input#message' : {
        'mousedown' : function(event){
            console.log('the mouse was pressed');
        },

        'touchstart' : function(event){
            console.log('a finger was pressed');
        }
    }
}
+4
source share
1

, . submit , :

Template.someForm.events({
  "submit": function() {
    /*...*/
  }
});

, , , . . , , , , "s" -, . :

Template.someTemplate.events({
  "keyup, click .save": function(event, template) {
    if (event.type === "keyup" && event.which !== 83) return;

    // save
  }
});

:

Template.input.events({
  "mousedown #message, touchstart #message" : function(event){
    // you can do different things by examining event.type
  }
});

, Meteor, , . , :

// Call it whatever you want
var transformEvents = function(selectorEvents) {
  return _.reduce(selectorEvents, function(meteorEvents, events, selector) {
    _.each(events, function(handler, eventType) {
      meteorEvents[eventType + " " + selector] = handler;
    });
    return meteorEvents;
  }, {});
};

, Meteor:)

jQuery, , Meteor , jQuery. , - jQuery-, , Meteor.

+8

All Articles