如何在更改Ember.js中的复选框时触发命名操作?任何帮助都将不胜感激。
这是我所拥有的。检查或取消选中复选框没有任何效果。
模板:
{{input type="checkbox" on="change" action="applyFilter"}}主计长:
actions: {
applyFilter: function() {
console.log("applyFilter");
}
}发布于 2014-06-11 05:19:47
使用观察者似乎是查看复选框更改的最简单方法。
模板
{{input type='checkbox' checked=foo}}代码
foo:undefined,
watchFoo: function(){
console.log('foo changed');
}.observes('foo')示例
http://emberjs.jsbin.com/kiyevomo/1/edit
或者,您也可以创建自己的复选框实现,该复选框发送一个操作。
自定义复选框
App.CoolCheck = Ember.Checkbox.extend({
hookup: function(){
var action = this.get('action');
if(action){
this.on('change', this, this.sendHookup);
}
}.on('init'),
sendHookup: function(ev){
var action = this.get('action'),
controller = this.get('controller');
controller.send(action, this.$().prop('checked'));
},
cleanup: function(){
this.off('change', this, this.sendHookup);
}.on('willDestroyElement')
});自定义视图
{{view App.CoolCheck action='cow' checked=foo}}示例
发布于 2015-11-10 13:00:15
我想把这个更新一下。在Ember 1.13.3+中,您可以使用以下内容:
<input type="checkbox"
checked={{isChecked}}
onclick={{action "foo" value="target.checked"}} />发布于 2015-04-16 01:24:23
Post Ember版本>= 1.13参见Kori John Roys' answer。
这是1.13之前的Ember版本
这是一个缺陷在烬的{{input type=checkbox}}助手。
请参阅https://github.com/emberjs/ember.js/issues/5433
我喜欢有个替补的想法。@Kingpin2k的解决方案可以工作,但是在全球范围访问视图是不可取的,使用观察者也不是很好。
在链接的github成员问题中,rwjblue建议使用组件版本:
App.BetterCheckboxComponent = Ember.Component.extend({
attributeBindings: ['type', 'value', 'checked', 'disabled'],
tagName: 'input',
type: 'checkbox',
checked: false,
disabled: false,
_updateElementValue: function() {
this.set('checked', this.$().prop('checked'));
}.on('didInsertElement'),
change: function(event){
this._updateElementValue();
this.sendAction('action', this.get('value'), this.get('checked'));
},
});模板中的示例用法(“选中”和“禁用”是可选的):
{{better-checkbox name=model.name checked=model.checked value=model.value disabled=model.disabled}}https://stackoverflow.com/questions/24154974
复制相似问题