我有一个动态创建的引导按钮组。所以我创建了一个具有不同图标和文本的按钮组件。
现在我已将操作分配给按钮内的范围。
样本组件.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/