使用 Ember.js 复选框输入助手触发更改事件的操作? [英] Trigger an action on the change event with Ember.js checkbox input helper?

查看:21
本文介绍了使用 Ember.js 复选框输入助手触发更改事件的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ember.js 中更改复选框时触发命名操作?任何帮助将不胜感激.

How can I fire a named action upon changing a checkbox in Ember.js? Any help will be greatly appreciated.

这是我所拥有的.选中或取消选中该复选框均无效.

Here is what I have. Checking or unchecking the checkbox has no effect.

模板:

{{input type="checkbox" on="change" action="applyFilter"}}

控制器:

actions: {
    applyFilter: function() {
        console.log("applyFilter");
    }
}

推荐答案

使用观察者似乎是观察复选框变化的最简单方法

using an observer seems like the easiest way to watch a checkbox changing

{{input type='checkbox' checked=foo}}

代码

  foo:undefined,
  watchFoo: function(){
    console.log('foo changed');
  }.observes('foo')

示例

http://emberjs.jsbin.com/kiyevomo/1/edit

或者您可以创建自己的发送操作的复选框的实现

Or you could create your own implementation of the checkbox that sends an action

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

示例

http://emberjs.jsbin.com/kiyevomo/6/edit

这篇关于使用 Ember.js 复选框输入助手触发更改事件的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆