简体   繁体   中英

Common EmberJS Route Events

Is there a way to have common events shared among multiple routes in Ember?

Sample:

App.CommonMenu = Ember.Mixin.create({
  events: {
    onDelete: function() {}
  }
});

App.MainMenu = Ember.Route.extend(App.CommonMenu, {
  events: {
    onSave: function() {}
  }
});


App.StoreMenu = Ember.Route.extend(App.CommonMenu, {
  events: {
    onSave: function(){}
  }
})

I'm only asking because I continue to get an error where, when a button is clicked, I get something similar to this:

Uncaught Error: Nothing handled the event 'onDelete'. 

A better way I can think of handling events globally across routes would be to define them in the events hash in your ApplicationRoute

App.ApplicationRoute = Ember.Route.extend({
  events: {
    myEvent: function() {
      // do stuff
    }
  }
});

App.MainMenu = Ember.Route.extend({
  events: {
    onSave: function() {}
  }
});

App.StoreMenu = Ember.Route.extend({
  events: {
    onSave: function(){}
  }
});

You could have also some route that handle the same event defined in your ApplicationRoute eg myEvent as long as you return true from that handler it will additionally bubble up all the way to the ApplicationRoute myEvent handler.

App.SomeOtherRoute = Ember.Route.extend({
  events: {
    myEvent: function(){
      // do stuff
      return true; // this will make the event bubble up all the way to the ApplicationRoute
    }
  }
});

Hope it helps.

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