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")}}
但函数会发生变化,即使表明可以重写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
}
如果您看到的是最新版本的ember3.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
,请查看我的答案是否有用?如果是,请核实。如果您仍然没有在我的答案下方获得解决方案评论。您觉得我的答案有用吗?如果是,请核实。如果您仍然没有在我的答案下方获得解决方案评论。