简体   繁体   English

灰烬:从组件到应用程序控制器的气泡动作

[英]Ember: Bubble action from component to application controller

I have a component in ember, which needs to send an action (with one parameter) to the application controller. 我在ember中有一个组件,它需要向应用程序控制器发送一个动作(带有一个参数)。 No matter where this component is rendered, it needs to call the exact same action, on application controller. 无论在何处呈现此组件,都需要在应用程序控制器上调用完全相同的动作。

application-controller 应用控制器

export default Ember.Controller.extend({
  actions: {
    addAlert: function(message) {
      this.set('message', message);
    },
    removeAlert: function(message) {
      this.set('message', message);
    }
  }
});

How do I handle this? 我该如何处理? From start, to end. 从开始到结束。

Actions don't bubble up through controllers, when an action is triggered it will go through the current route's controller, if nothing handles it there it bubbles up to the current route all the way up to the top level route (application). 动作不会在控制器中冒泡,当触发动作时,它将通过当前路径的控制器,如果没有任何处理,它将一直冒泡到当前路径,一直到顶层路径(应用程序)。

If that action has to set a property on the controller you can set it directly from the application route (although it is not recommended). 如果该操作必须在控制器上设置属性,则可以直接从应用程序路由中设置它(尽管不建议这样做)。

// routes/application.js
actions {
  addAlert(message) {
      this.controller.set('message', message);
    },
    removeAlert(message) {
      this.controller.set('message', message);
    }
}

For more information read up on action bubbling . 有关更多信息,请阅读有关动作冒泡的信息

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

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