如何在更改Ember.js中的复选框时触发命名操作?任何帮助将不胜感激.
这就是我所拥有的.选中或取消选中该复选框无效.
模板:
{{input type="checkbox" on="change" action="applyFilter"}}
Run Code Online (Sandbox Code Playgroud)
控制器:
actions: {
applyFilter: function() {
console.log("applyFilter");
}
}
Run Code Online (Sandbox Code Playgroud)
Kor*_*oys 64
我想发布一个更新.在Ember 1.13.3+中,您可以使用以下内容:
<input type="checkbox"
checked={{isChecked}}
onclick={{action "foo" value="target.checked"}} />
Run Code Online (Sandbox Code Playgroud)
Kin*_*n2k 24
使用观察者似乎是观看复选框更改的最简单方法
{{input type='checkbox' checked=foo}}
Run Code Online (Sandbox Code Playgroud)
foo:undefined,
watchFoo: function(){
console.log('foo changed');
}.observes('foo')
Run Code Online (Sandbox Code Playgroud)
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')
});
Run Code Online (Sandbox Code Playgroud)
{{view App.CoolCheck action='cow' checked=foo}}
Run Code Online (Sandbox Code Playgroud)
http://emberjs.jsbin.com/kiyevomo/6/edit
hew*_*ism 12
后Ember版> = 1.13见Kori John Roys的回答.
这是1.13之前的Ember版本
这是ember {{input type=checkbox}}助手中的一个错误.
请参阅https://github.com/emberjs/ember.js/issues/5433
我喜欢有替身的想法.@ Kingpin2k的解决方案可行,但全局访问视图已被弃用,使用观察者并不是很好.
在链接的github ember问题中,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'));
},
});
Run Code Online (Sandbox Code Playgroud)
模板中的示例用法('checked'和'disabled'是可选的):
{{better-checkbox name=model.name checked=model.checked value=model.value disabled=model.disabled}}
Run Code Online (Sandbox Code Playgroud)
对于使用Ember> 2.x的用户:
{{input
change=(action 'doSomething')
type='checkbox'}}
Run Code Online (Sandbox Code Playgroud)
和动作:
actions: {
doSomething() {
console.warn('Done it!');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31581 次 |
| 最近记录: |