简体   繁体   English

Ember.js(v2.4.5)组件没有传递第二个动作

[英]Ember.js (v2.4.5) component isn't passing up second action

I've got a very simple component which has two actions, start and stop. 我有一个非常简单的组件,它有两个动作,即开始和停止。 These are fired when clicking buttons. 单击按钮时将触发它们。 The first action is bubbled up to the route and works as expected, however the second action is fired in the component, but never makes it to the route. 第一个动作冒泡到路线并按预期工作,但是第二个动作在组件中触发,但从未到达路线。

I have just started playing around with Ember, but I assume components can preform more than one action? 我刚刚开始玩Ember,但是我想组件可以执行多个动作?

There are no errors in the console, the button just doesn't do anything and the console log from the route is never displayed. 控制台中没有错误,按钮只是不执行任何操作,并且从路由中不会显示控制台日志。

Component actions 组件动作

actions: {

  start() {
    console.log('component start called');
    this.sendAction('start', this.get('item'));
  },

  stop() {
    console.log('component stop called');
    this.sendAction('stop', this.get('item'));
  }
}

Route actions 路线动作

actions: {
    start (server) {
        console.log('route start called');
        server.set("settings.amazonTask", 'start');
        server.save();
    },

    stop (server) {
        console.log('route stop called');
        server.set('settings.amazonTask', 'stop');
        server.save();
    }
}

Template 模板

<button type="button"
        class="btn btn-default btn-sm" {{action "start"}}>
    Turn on
</button>


<button type="button"
        class="btn btn-default btn-sm" {{action "stop"}}>
    Turn off
</button>

You have to give the actions down to your component: 您必须将操作归结为组件:

{{my-component start=(action 'start') stop=(action 'stop')}}

And then you can call then with sendAction . 然后,您可以使用sendAction进行调用。

But I highly recommend to use the new syntax and directly access the actions on the attrs object. 但是我强烈建议使用新语法并直接访问attrs对象上的操作。 This is much clearer and makes clear whats happening: 这更加清楚,可以清楚地了解发生了什么:

this.attrs['start'](this.get('item'))

Actually the action helper just gets an action from the actions object and bounds it to the current context. 实际上, action助手只是从actions对象中获取一个动作并将其绑定到当前上下文。 The result of that can be given down to the component and then be called from there, in the context where you executed the action helper. 可以将结果传递给组件,然后在执行action帮助器的上下文中从该组件调用该结果。

Note that calling the action helper on an already created action will not rebound the action but just pass it though. 请注意,在已创建的动作上调用action助手将不会反弹动作,而只是通过它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM