繁体   English   中英

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

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

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

现在,我已将操作分配给按钮内的跨度。

样品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
  }
 }

样品component.hbs

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

我试图this.set('action','triggerStatus')didInsertElement ,但它没有工作。 有人可以建议我如何为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>

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

暂无
暂无

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

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