最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use a knockoutjs protected observable with checkboxes? - Stack Overflow

programmeradmin0浏览0评论

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:

  1. Run the fiddle (default value for the checkbox is true)
  2. Uncheck the checkbox
  3. Click "Commit"
  4. Note that the value now shows as false (this is the correct behavior)
  5. Run the fiddle again (default value for the checkbox is true)
  6. Uncheck the checkbox then check it immediately (before clicking "Commit")
  7. Click "Commit"
  8. 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:

  1. Run the fiddle (default value for the checkbox is true)
  2. Uncheck the checkbox
  3. Click "Commit"
  4. Note that the value now shows as false (this is the correct behavior)
  5. Run the fiddle again (default value for the checkbox is true)
  6. Uncheck the checkbox then check it immediately (before clicking "Commit")
  7. Click "Commit"
  8. 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
Add a ment  | 

1 Answer 1

Reset to default 11

protectedObservable'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

发布评论

评论列表(0)

  1. 暂无评论