Why is the checkbox in knockoutJs written only when it is checked?

I use a special extended version of Ko that uses functions like commit () / peek () for the dependent observable, so I only save the data in the form if it is bound to the database (in order) and will return to cancellation.

The problem I am facing is that when I check the box, the recording function is correctly called the protected observable. However, if you uncheck the box, the write function is not called, so when the model is sent to my MVC controller, the boolean flag is still TRUE.

Why doesn't knockout write FALSE?

UPDATE QUESTION WITH CODE:

Please check out this JsFiddle to demonstrate: http://jsfiddle.net/b2Qu2/3/

Minor issue

Please note that there is one other problem with the demo - for some reason, when I check / uncheck the peeked value is not updated in the user interface, even if it is bound to dependOffable. You can still see the value by clicking the "peek" button.

Main problem

To reproduce the problem:

1) Click 'peek' button: Shows FALSE - CORRECT

2) Check IsAdmin checkbox

3) Click 'peek' again: Shows TRUE - CORRECT

4) Uncheck IsAdmin

5) Click 'peek' again: SHOWS TRUE - INCORRECT!!

Scenario example

, Admin. admin , /, admin . , , ? protectedObservable , , commit() , . , .

+5
1

, , , , , , . , protectedObservable . , true, , , .

, , - :

ko.protectedObservable = function (initialValue) {
    //private variables
    var _actual = ko.observable(initialValue),
        _temp = ko.observable(initialValue);

    //access to temp value
    _actual.temp = _temp;

    //commit the temporary value to our observable, if it is different
    _actual.commit = function () {        
        if (_temp() !== _actual()) {
            _actual(_temp());
        }
    };

    //notify subscribers to update their value with the original
    _actual.reset = function () {
        _actual.valueHasMutated();
        _temp(_actual());
    };

    return _actual;
};

field1 field1.temp. , , .

: http://jsfiddle.net/rniemeyer/BwDYE/

+3

All Articles