2013-08-06 46 views
0

有沒有一種方法可以在Ember中的多條路線之間共享公共事件?常見EmberJS路線事件

樣品:

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(){} 
    } 
}) 

我只問,因爲我不斷地得到一個錯誤的位置,一個按鈕被按下時,我得到類似這樣:

Uncaught Error: Nothing handled the event 'onDelete'. 
+0

你能分享你觸發事件的模板嗎? –

回答

1

更好的辦法我可以想到,處理全球各種路線的事件將會在您的events哈希中定義它們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(){} 
    } 
}); 

您也可能有一些處理您的ApplicationRoute中定義的相同事件的路線,例如myEvent只要你從那個處理程序返回true它將會一直冒泡到ApplicationRoutemyEvent處理程序。

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 
    } 
    } 
}); 

希望它有幫助。