Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Checkbox 什么是正确的ember.js-way来响应更改的复选框_Checkbox_Ember.js - Fatal编程技术网

Checkbox 什么是正确的ember.js-way来响应更改的复选框

Checkbox 什么是正确的ember.js-way来响应更改的复选框,checkbox,ember.js,Checkbox,Ember.js,我有这样一个复选框: {{input type="checkbox" checked=property}} 当我单击复选框时,属性已更改。现在我想调用一个函数来持久化属性。但最好的方法是什么呢 我可以把一个观察者绑在财产上 我可以将一个操作绑定到输入或单击 {{input ... input=(action "actionName") click=(action "actionName")}} 这不起作用,因为调用操作时属性仍然具有旧值 扩展类复选框: import Checkbox fro

我有这样一个复选框:

{{input type="checkbox" checked=property}}
当我单击复选框时,属性已更改。现在我想调用一个函数来持久化属性。但最好的方法是什么呢

  • 我可以把一个观察者绑在财产上
  • 我可以将一个操作绑定到输入或单击

    {{input ... input=(action "actionName") click=(action "actionName")}}
    
    这不起作用,因为调用操作时属性仍然具有旧值

  • 扩展类复选框:

    import Checkbox from '@ember/component/checkbox';
    Checkbox.reopen({
        onChange: null,
        change() {
            this._super();
            if (this.onChange) this.onChange();
        }
    });
    ...
    {{input ... onChange=(action "actionName")}}
    
    但函数会发生变化,即使表明可以重写

  • 我可以将一个操作绑定到didUpdate或didupdateatrs,但该操作被调用两次。在我的例子中,这不是问题,因为属性是模型的一部分,所以我可以在操作中调用
    model.get('hasDirtyAttributes')
    [Update]在我的测试用例中,操作被调用了两次,但在我的实际代码中,它只被调用一次,所以这似乎是最好的解决方案?[/Update]


  • 那么,什么是合适的ember.js方法呢?

    我个人更喜欢用复选框进行单向绑定。我使用本机
    输入
    ,如下所示:

    <input type="checkbox" checked={{myValue}} onclick={{action (mut myValue) value="target.checked"}}>
    
    这样您就可以与以下api一起使用

    {{#paper-checkbox value=value1 onChange=(action (mut value1))}}
        A checkbox: {{value1}}
    {{/paper-checkbox}}
    

    如果您想要一个比本机更好的复选框,您可以看到以及如何实现and以获得灵感

    我个人更喜欢使用复选框的单向绑定。我使用本机
    输入
    ,如下所示:

    <input type="checkbox" checked={{myValue}} onclick={{action (mut myValue) value="target.checked"}}>
    
    这样您就可以与以下api一起使用

    {{#paper-checkbox value=value1 onChange=(action (mut value1))}}
        A checkbox: {{value1}}
    {{/paper-checkbox}}
    
    如果您想要一个比native更好看的复选框,您可以看到以及如何实现and以获得灵感。请看一下我的示例

    我建议使用
    input
    helper

    ({input type="checkbox" click=(action "setProperty") checked=my_property}}
    
    在操作内切换属性

    setProperty() {
      // toggle the property on click
      this.toggleProperty("my_property");
      console.log(this.get('my_property')); // returns true or false based on the checked value
      .. // do whatever stuff you want
    }
    
    如果您看到的是最新版本的ember
    3.9
    ,请查看我的示例

    我建议使用
    input
    helper

    ({input type="checkbox" click=(action "setProperty") checked=my_property}}
    
    在操作内切换属性

    setProperty() {
      // toggle the property on click
      this.toggleProperty("my_property");
      console.log(this.get('my_property')); // returns true or false based on the checked value
      .. // do whatever stuff you want
    }
    

    如果您看到的是最新版本的ember
    3.9
    ,请查看

    我的答案是否有用?如果是,请核实。如果您仍然没有在我的答案下方获得解决方案评论。您觉得我的答案有用吗?如果是,请核实。如果您仍然没有在我的答案下方获得解决方案评论。