admin 管理员组文章数量: 1086019
I'm using knockoutjs with protected observable and am having an issue with checkboxes. One implementation of protected observables can be found here but there are several that I've seen that are very similar.
A jsFiddle that demonstrates my issue can be found here. Here is a portion of the fiddle.
var ViewModel = function() {
var self = this;
self.protectedBool = ko.protectedObservable(true);
selfmit = function(){
komitProtectedObservables(self);
};
self.rollback = function() {
ko.rollbackProtectedObservables(self);
};
};
$(function() {
ko.applyBindings(new ViewModel());
});
To duplicate, do the following:
- Run the fiddle (default value for the checkbox is true)
- Uncheck the checkbox
- Click "Commit"
- Note that the value now shows as false (this is the correct behavior)
- Run the fiddle again (default value for the checkbox is true)
- Uncheck the checkbox then check it immediately (before clicking "Commit")
- Click "Commit"
- You'll see the value will be set to false/unchecked even though it was checked when you clicked "Commit".
The "write" event in the puted observable inside the protectedObservable definition does not get fired the second time you change the checkbox and thus when the value is mited, it's mitting the incorrect value.
Also note that the protectedObservable works perfectly for strings. Any guidance would be greatly appreciated.
I'm using knockoutjs with protected observable and am having an issue with checkboxes. One implementation of protected observables can be found here but there are several that I've seen that are very similar.
A jsFiddle that demonstrates my issue can be found here. Here is a portion of the fiddle.
var ViewModel = function() {
var self = this;
self.protectedBool = ko.protectedObservable(true);
self.mit = function(){
ko.mitProtectedObservables(self);
};
self.rollback = function() {
ko.rollbackProtectedObservables(self);
};
};
$(function() {
ko.applyBindings(new ViewModel());
});
To duplicate, do the following:
- Run the fiddle (default value for the checkbox is true)
- Uncheck the checkbox
- Click "Commit"
- Note that the value now shows as false (this is the correct behavior)
- Run the fiddle again (default value for the checkbox is true)
- Uncheck the checkbox then check it immediately (before clicking "Commit")
- Click "Commit"
- You'll see the value will be set to false/unchecked even though it was checked when you clicked "Commit".
The "write" event in the puted observable inside the protectedObservable definition does not get fired the second time you change the checkbox and thus when the value is mited, it's mitting the incorrect value.
Also note that the protectedObservable works perfectly for strings. Any guidance would be greatly appreciated.
Share Improve this question asked Apr 12, 2013 at 20:46 rhoadscerhoadsce 2782 silver badges6 bronze badges 3- I don't have a proper answer yet. So I just post this very hacky workaround as a ment: jsfiddle/CyHyW – nemesv Commented Apr 12, 2013 at 22:01
-
1
protectedObservable
's implementation is very old. I guess it works with older version of KO (< 2) only. For new KO refer the editor pattern implemented by the same author (Ryan Niemeyer) - knockmeout/2013/01/simple-editor-pattern-knockout-js.html – Rango Commented Apr 13, 2013 at 15:12 - 1 @f_martinez You should post that as an answer so OP can accept it, helping future visitors easily spot the answer. – Jeroen Commented Apr 30, 2013 at 10:30
1 Answer
Reset to default 11protectedObservable
's implementation is very old. It works with older versions of KO (<2) only.
For new KO versions refer the editor pattern implemented by the same author (Ryan Niemeyer) - http://www.knockmeout/2013/01/simple-editor-pattern-knockout-js.html
本文标签: javascriptHow to use a knockoutjs protected observable with checkboxesStack Overflow
版权声明:本文标题:javascript - How to use a knockoutjs protected observable with checkboxes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744016818a2519069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论