Skip to content

Instantly share code, notes, and snippets.

@mlippens
Created July 31, 2015 08:05
Show Gist options
  • Save mlippens/2898383e50b3cc0bca8d to your computer and use it in GitHub Desktop.
Save mlippens/2898383e50b3cc0bca8d to your computer and use it in GitHub Desktop.
/**
* iCheck integration bindingHandler
*
* This handler will initialise checkboxes and update them accordingly in both ways
*
*/
ko.bindingHandlers.iCheck = {
/**
* We set an event listener: if a checkbox is changed, we update the observable accordingly.
* @param el
* @param valueAccessor
*/
init: (el, valueAccessor: Function) => {
var observable = valueAccessor();
$(el).on("ifChanged", function() {
observable(this.checked);
});
},
/**
* If the observable is changed, we update the checkbox UI accordingly.
* @param el
* @param valueAccessor
*/
update: (el, valueAccessor) => {
var val : boolean = ko.utils.unwrapObservable(valueAccessor());
if (val) {
$(el).iCheck('check');
} else {
$(el).iCheck('uncheck');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment