javascript - Ember.js 将操作分配给组件 js 文件中的组件

标签 javascript ember.js ember-cli

我有一个动态创建的引导按钮组。所以我创建了一个具有不同图标和文本的按钮组件。

现在我已将操作分配给按钮内的范围。

样本组件.js

export default Ember.Component.extend({
  tagName: 'button',
  actions:{
    triggerStatus:function(){
        this.sendAction('triggerStatus',this.get('dataId'),this.get('type'));
    }
  },
  didInsertElement: function(){
    //is it possible to assign action here
  }
 }

样本组件.hbs

<span class="glyphicon glyphicon-{{icon}}" aria-hidden="true" {{action "triggerStatus"}}></span>

我在 didInsertElement 内尝试了 this.set('action','triggerStatus') 但没有成功。 有人可以建议我如何将操作分配给 javascript 文件本身中的组件吗?可以这样做吗?

最佳答案

终于找到问题的解决方案了。解决方案非常简单,我们只需在组件内添加一个 click 处理程序,然后从那里调用 sendAction 方法即可。请在下面找到示例代码片段

export default Ember.Component.extend({
  tagName: 'button',
  //rewrite the following action to
  /*actions:{
    triggerStatus:function(){
      this.sendAction('triggerStatus',this.get('dataId'),this.get('type'));
    }
  }*/,
  //to a component click handler
  click: function(){
    this.sendAction('triggerStatus',this.get('dataId'),this.get('type'));
  }
}

并从sample-component.hbs文件中的span标记中删除了该操作

<span class="glyphicon glyphicon-{{icon}}" aria-hidden="true"></span>

现在按钮组中的每个按钮根据类型具有不同的操作

关于javascript - Ember.js 将操作分配给组件 js 文件中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965072/

相关文章:

javascript - EmberJS 重置警告 : Library is already registered with Ember

ember.js - ember 组件内存泄漏

javascript - Ember 组件事件未触发

php - Ember CLI : index. php 而不是 index.html

javascript - 我可以在 Ember 2.1 中升级 jQuery 2 吗?

javascript - 更改 php 表的背景颜色

javascript - 使用 pg 管理从 nodejs 到 postgres 的许多连接

javascript - 滚动时固定标题

ember.js - 弃用 : Controller#needs is deprecated, 请改用 Ember.inject.controller()

javascript - Webix(protoUI)中建议列表中的表单