Tudor Vintilescu
Tudor Vintilescu

Reputation: 1460

Action hook in Ember.js

I would like to use a Mixin to intercept a list of actions (before the specific action function is executed on the controller, route or component). Is there any way I can do this?

The code would look something like:

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

Looking for a way to have intereptorFunction called before sampleAction.

Thank you.

Upvotes: 1

Views: 257

Answers (1)

blessanm86
blessanm86

Reputation: 31779

Here is a working demo where the handler in the mixin is executed first after the handler in the controller is called. To call the controllers handler, we need to call 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
);

Upvotes: 1

Related Questions