Ember.js在component' js文件中给组件分配动作
Ember.js assign action to a component inside component's js file
我有一个动态创建的引导按钮组。所以我创建了一个按钮组件,它有不同的图标和文本。
现在我已经将动作分配给按钮内部的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>
现在按钮组中的每个按钮都有不同的动作基于类型
相关文章:
- 如何创建带有插槽的vue js组件预加载程序
- ember-js组件初始化不同的函数
- 将自定义脚本加载到Vue.js组件中
- 在多个react.js组件中呈现json数据
- 以最封装的方式封装/封装ScalaJS react js组件(react stick)
- 如何将图表与react js组件集成
- React JS 组件在 Meteor 中多次渲染
- React Js 组件在状态更改后只渲染一次
- React.js组件中的状态更新在我的页面下留下了空白
- 从淘汰js组件中获取数据
- 无需CSS即可设计React.js组件的最佳方式
- Vue.js组件与Vuex.js(而不是Vue-i18n.js)
- 反应.js组件:用户定义的属性.我做得对吗
- 类似单选按钮的行为 Vue.js 组件
- 当有多个 React.js 组件实例具有数据请求时如何处理缓存
- 是否有在 Zurb Foundation 4 中有条件地加载 JS 组件的最佳实践
- 余烬JS组件观察器不起作用
- 反应.js - 组件将接收道具被击中两次
- 模块化 JS 组件中的委派模式
- ES2015 单例或服务提供商或模块,用于敲除.js组件