Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not calling Init. </p> <p>Simply proxy the init function for checked in your nullableChecked init function like you did in the update.</p> <pre><code>ko.bindingHandlers.nullableChecked = { init: function(element, valueAccessor) { ko.bindingHandlers.checked.init(element, valueAccessor); }, update: function (element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); if (value == null) element.indeterminate = true; ko.bindingHandlers.checked.update(element, valueAccessor); } }; </code></pre> <p>Without the init, it's never actually setting up a "click" binding on the checkbox to tell knockout that something has changed. If you look at the debug code (<a href="http://knockoutjs.com/downloads/knockout-2.2.1.debug.js" rel="nofollow">http://knockoutjs.com/downloads/knockout-2.2.1.debug.js</a>), you'll see that init uses jQuery to set up a 'click' event on the checkbox to update the observable when the value changes.</p> <pre><code>ko.bindingHandlers['checked'] = { 'init': function (element, valueAccessor, allBindingsAccessor) { var updateHandler = function() { var valueToWrite; if (element.type == "checkbox") { valueToWrite = element.checked; } else if ((element.type == "radio") &amp;&amp; (element.checked)) { valueToWrite = element.value; } else { return; // "checked" binding only responds to checkboxes and selected radio buttons } var modelValue = valueAccessor(), unwrappedValue = ko.utils.unwrapObservable(modelValue); if ((element.type == "checkbox") &amp;&amp; (unwrappedValue instanceof Array)) { // For checkboxes bound to an array, we add/remove the checkbox value to that array // This works for both observable and non-observable arrays var existingEntryIndex = ko.utils.arrayIndexOf(unwrappedValue, element.value); if (element.checked &amp;&amp; (existingEntryIndex &lt; 0)) modelValue.push(element.value); else if ((!element.checked) &amp;&amp; (existingEntryIndex &gt;= 0)) modelValue.splice(existingEntryIndex, 1); } else { ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'checked', valueToWrite, true); } }; ko.utils.registerEventHandler(element, "click", updateHandler); // IE 6 won't allow radio buttons to be selected unless they have a name if ((element.type == "radio") &amp;&amp; !element.name) ko.bindingHandlers['uniqueName']['init'](element, function() { return true }); }, </code></pre> <p><strong>Edit:</strong> Here's a Fiddle: <a href="http://jsfiddle.net/cclose/NFfVn/" rel="nofollow">http://jsfiddle.net/cclose/NFfVn/</a></p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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