简体   繁体   中英

meteor listen for multiple events fire just once

Meteor offers to define eventHandlers for templates. I have a text input that a user can type, but also paste in etc.

Sofar I have used:

Template.myTemplate.events({
    'keyup #inputfield': function() {
         DO SOMETHING HERE
     }
});

Is it possible to define something similar to jquery's "on" function?

$('#inputfield').on('change keypress paste focus textInput input', function () 
{ 
    DO SOMETHING HERE
});

This will fire only once regardless of how many of the events occur at the same time.

You can use stopImmediatePropagation to stop additional handers:

Template.myTemplate.events({
    'keyup #inputfield': function(event) {
         event.stopImmediatePropagation();
     }
});

or you could use jQuery plus the rendered command to achieve the same thing:

Template.myTemplate.rendered(function() {
  $(this.find('#inputField')).on('change keypress paste focus textInput input', function () 
  { 
      DO SOMETHING HERE
  });
});

_.throttle should do the trick:

var handler = _.throttle(function(event) {
    ...
}, 1, {leading: false});


Template.myTemplate.events({
    'event event anotherEvent': handler,
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM