send()和triggerAction()之间的区别

时间:2013-12-08 11:58:51

标签: ember.js

我正在从视图向其父视图发送事件。现在我有两个有效的解决方案:

App.View = Ember.View.extend({
  somethingHappened: function() {
    this.get('parentView').send('anAction');
  })
});

App.View = Ember.View.extend(Ember.ViewTargetActionSupport, {
  somethingHappened: function() {
    this.triggerAction({
      action: 'anAction',
      target: this.get('parentView')
    });    
  })
});

这两种方法有什么区别?我无法从API中弄清楚我应该使用triggerAction mixin中的ViewTargetActionSupport

2 个答案:

答案 0 :(得分:4)

如果查看source for triggerAction,您会看到它在内部使用send()。在我看来,triggerAction似乎是围绕send的一个很好的包装器,虽然我之前没有使用它,只是依靠send

答案 1 :(得分:1)

triggerAction bubbles an event. send and sendAction require an action coming up from the component itself.

triggerAction in the component's controller will work with only {{component-name}} in the template.

Where as, send requires something like {{component-name onConfirm=(action='doItNow')}}