2014-07-08 58 views
1

我想使用Mixin拦截动作列表(在控制器,路由或组件上执行特定动作函数之前)。有什么办法可以做到这一点?Ember.js中的动作钩子

的代码看起来是这样的:

App.MyMixin = Em.Mixin.create({ 

    interceptorFunction : function(actionName){ 
     console.log('intercepted action: ' + actionName); 

    } 

}); 

App.TheInterceptedComponent = Em.Component.extend(App.MyMixin, { 
    actions : { 
     sampleAction : function() { 
       console.log('sampleAction std handler called'); 
     } 
    } 
}); 

寻找办法有intereptorFunction sampleAction之前调用。

谢谢。

回答

1

Here is a working demo其中调用控制器中的处理程序之后,首先执行mixin中的处理程序。要调用控制器处理程序,我们需要调用this._super();

App.MyMixin = Em.Mixin.create({ 
    actions: { 
    actionHandler: function() { 
     alert('Handled In Mixin'); 
     this._super(); 
    } 
    } 
}); 

App.IndexController = Em.ObjectController.extend({ 
    actions: { 
     actionHandler: function() { 
     alert('Handled In Controller'); 
     } 
    } 
    }, 
    App.MyMixin 
); 
+0

很好,工作得很好。没有考虑重新排列论据。谢谢 –