javascript - Ember : Bubble action from component to application controller

标签 javascript ember.js ember-cli

我在 ember 中有一个组件,它需要向应用程序 Controller 发送一个 Action (带有一个参数)。无论该组件在哪里呈现,它都需要在应用程序 Controller 上调用完全相同的操作。

应用程序 Controller

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

我该如何处理?从头到尾。

最佳答案

Action 不会通过 Controller 向上冒泡,当一个 Action 被触发时,它将通过当前路由的 Controller ,如果那里没有任何东西处理它,它会向上冒泡到当前路由,一直到顶层路由(应用程序) .

如果该操作必须在 Controller 上设置一个属性,您可以直接从应用程序路由中设置它(尽管不推荐这样做)。

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

有关更多信息,请阅读 action bubbling .

关于javascript - Ember : Bubble action from component to application controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853437/

相关文章:

javascript - 跟踪并记录所有调用树/图的 javascript 函数?

javascript - Gulp 中的 Grunt 风格模板?

ember.js - 从 View 访问模型关联

ember.js - 如何手动重用错误路由?

javascript - #each 循环中的 Handlebars.js 助手返回相同的结果?

java - 如何使用ember部署的jar

javascript - 在新窗口中打开一个 div - 单击按钮

Javascript通过匹配字符串对数组进行排序

ember.js - Ember-cli ProxyPass

Ember.js:直接加载相关模型