27

I have this view

  App.ApplicationView = Em.View.extend({
    templateName: 'application',

    actions: {
      myAction: function() {
        //
      }
    }
  });

Suppose I want to trigger manually that action from another view method, such as didInsertElement, like:

  App.ApplicationView = Em.View.extend({
    templateName: 'application',

    actions: {

      sidebarShowHome: function() {
        this.set('sidebarIsHome', true);
        this.set('sidebarIsNotifications', false);
        this.set('sidebarIsArchive', false);
      },
    },

    didInsertElement: function() {
      this.actions.sidebarShowHome();
    }
  });

How could I do it? this.actions is undefined from within a view method.

Liam
  • 27,717
  • 28
  • 128
  • 190
Flavio Copes
  • 4,131
  • 4
  • 27
  • 30

3 Answers3

23

You can use the TargetActionSupport mixin along with this.triggerAction:

App.ApplicationView = Ember.View.extend(Ember.TargetActionSupport, {
  templateName: 'application',
  actions: {
    myAction: function() {
      console.log('myAction');
    }
  },
  didInsertElement: function() {
    this.triggerAction({
      action:'myAction',
      target: this
    });
  }
});

By default using this.triggerAction() without parameters will send the action to the controller, however if you need to target a different destination define the target option as shown in the example.

Working demo.

Hope it helps.

intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • 1
    @intuitivepixel Is there a way to pass an argument to the action? – Jon Koops Nov 27 '13 at 15:19
  • @JonKoops, to really, to pass arguments you should use the context and the controller that can be passed along: http://emberjs.com/api/classes/Ember.TargetActionSupport.html#method_triggerAction and grab from there your needed values/params etc. – intuitivepixel Nov 27 '13 at 18:52
  • 2
    Great answer intuitivepixel, how would you pass in a parameter to triggerAction if the action it was calling took in a parameter. For e.g myAction(param), then how can we pass a parameter when this action is invoked from triggerAction ? – Parijat Kalia Dec 03 '13 at 01:23
  • @Parijat Kalia, seconding your question! Edit: Found the answer here: http://stackoverflow.com/questions/20340963/passing-parameters-to-ember-triggeraction-which-calls-an-action-that-takes-param – DelphiLynx Dec 04 '13 at 10:39
  • @vanthome you should add your comment as an answer – rxgx Dec 04 '13 at 19:50
  • 1
    @ParijatKalia I know ;-) Thanks for that question! – DelphiLynx Dec 05 '13 at 07:24
  • @Karl but it will work if you extend Ember.ViewTargetActionSupport (not done by default). – Karl Glaser Jan 29 '14 at 05:56
15

To call an action in the same controller you can use send.

this.send('nameOfAction', someParams);

To call an action in the parent controller from a component use sendAction.

this.sendAction('nameOfAction', someParams);
Nelu
  • 16,644
  • 10
  • 80
  • 88
12

The triggerAction() method only exists, if the view uses the Ember.TargetActionSupport mixin. If this is not the case, use this.get('controller').send('action'); instead.

vanthome
  • 4,816
  • 37
  • 44
  • 1
    triggerAction() totally exists. You are reading the question incorrectly, this.get('controller').send('action') will call an action declared in the actions object within the controller, triggerAction() is used for triggering an action from within the same view which is what the question states. – Parijat Kalia Dec 05 '13 at 16:19
  • triggerAction doesn't exist - if you're not using the Ember.TargetActionSupport mixin – nont May 02 '14 at 19:16
  • 1
    `triggerAction` does exist, but it certainly makes more sense to use [`send`](http://emberjs.com/api/classes/Ember.Controller.html#method_send). – wbyoung Jun 27 '14 at 01:19