pian0
pian0

Reputation: 847

KnockoutJS 'checked' binding not applied in customBinding?

I have a checkbox being controller by a Knockout observable, but in a custom binding, the element is not checked.

<input type="checkbox" data-bind="checked: isChecked, highlightIfChecked: 'test'"/>

The element is checked and unchecked on the page, but in the highlightIfChecked custom binding, it is not. I'm using $(elem).is(":checked").

JSFiddle: http://jsfiddle.net/JgLck/

How can I get the element to be checked in the custom binding?

Upvotes: 1

Views: 90

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114802

The setting of the element's checked value happens in the checked binding's update function. Currently, all of the init functions run before the update functions for the bindings on an element.

So, if you switched your custom binding to use update instead of init, then you would see the right value.

Also, you would need to change your isChecked value into an observable, if you want the binding to trigger again. Note that in KO 3.0, bindings will be processed independently, so your custom binding would need to access isChecked to create a dependency.

http://jsfiddle.net/rniemeyer/eXEmM/

Upvotes: 3

Related Questions