Ember.js在component' js文件中给组件分配动作

Ember.js assign action to a component inside component's js file

本文关键字:js 组件 分配 component Ember 文件      更新时间:2023-09-26

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

现在我已经将动作分配给按钮内部的span。

sample-component.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
  }
 }

sample-component.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'));
  }
}

并从示例组件中的span标记中删除操作。哈佛商学院文件

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

现在按钮组中的每个按钮都有不同的动作基于类型