Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a knockoutjs protected observable with checkboxes?
    primarykey
    data
    text
    <p>I'm using knockoutjs with protected observable and am having an issue with checkboxes. One implementation of protected observables can be found <a href="http://www.knockmeout.net/2011/03/guard-your-model-accept-or-cancel-edits.html" rel="noreferrer">here</a> but there are several that I've seen that are very similar.</p> <p>A jsFiddle that demonstrates my issue can be found <a href="http://jsfiddle.net/rhoadsce/r9pKr/1/" rel="noreferrer">here</a>. Here is a portion of the fiddle.</p> <pre><code>var ViewModel = function() { var self = this; self.protectedBool = ko.protectedObservable(true); self.commit = function(){ ko.commitProtectedObservables(self); }; self.rollback = function() { ko.rollbackProtectedObservables(self); }; }; $(function() { ko.applyBindings(new ViewModel()); }); </code></pre> <p>To duplicate, do the following:</p> <ol> <li>Run the fiddle (default value for the checkbox is true)</li> <li>Uncheck the checkbox</li> <li>Click "Commit"</li> <li>Note that the value now shows as false (this is the correct behavior)</li> <li>Run the fiddle again (default value for the checkbox is true)</li> <li>Uncheck the checkbox then check it immediately (before clicking "Commit")</li> <li>Click "Commit"</li> <li>You'll see the value will be set to false/unchecked even though it was checked when you clicked "Commit".</li> </ol> <p>The "write" event in the computed observable inside the protectedObservable definition does <em>not</em> get fired the second time you change the checkbox and thus when the value is commited, it's committing the incorrect value.</p> <p>Also note that the protectedObservable works perfectly for strings. Any guidance would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload