简体   繁体   中英

Calling custom events on Meteor 1.2

I am trying to call a custom event that I made on meteor from my router controller with no success. This is the event that I am trying to trigger:

Template.foo.events ({

"click button.my_event" : function (e) {
//event
}
})

I tried to call it using the jQuery method .trigger() like so:

action : function () {
$('.my_event').trigger('click button.my_event');  
}

Which failed. Later I thought that the event itself needs to be made using the jQuery event layer in order to use trigger() so I tried that:

"click button.my_event" : $(".my_event").on("click",function (e) { 
//event
}),

This approach has also failed. Am I missing something?

Edit: I misspoke when I refereed to it as a custom event. What I really meant was a regular click event that activates a sequence of methods (like any regular event).

You specified event type as click . That's why it doesn't work.

Here's working example:

<template name="Test">
    <button class="trigger">Trigger</button>
    <button class="test-button">Test</button>
</template>

JS

Template.Test.events({
  'click .trigger': function (event, tmpl) {
    tmpl.$('.test-button').trigger('myEvent');
  },

  //custom event handler
  'myEvent .test-button': function (event, tmpl) {
    console.log('Hello, myEvent');
  }
});

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