Skip to content

Instantly share code, notes, and snippets.

@jonathancounihan
Created June 25, 2014 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathancounihan/ffbbe2d16dd02cfdf984 to your computer and use it in GitHub Desktop.
Save jonathancounihan/ffbbe2d16dd02cfdf984 to your computer and use it in GitHub Desktop.
knockout binding for iCheck
ko.bindingHandlers.iCheck = {
init: function (element, valueAccessor) {
var $el = $(element);
var observable = valueAccessor();
$el.iCheck({
radioClass: 'iCheck_radio_class',
inheritClass: true
});
$el.on('ifClicked', function (e) {
var val = $(e.target).val();
observable(val);
console.log(e.type + ' callback');
console.log("ID: " + e.target.id);
console.log("name: " + e.target.name); // Note that name must be set on the radio buttons for this to work!!!
console.log("value: " + val);
});
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// This update handles both the reverting of values from cancelling edits, and the initial value setting.
var $el = $(element);
var value = ko.unwrap(valueAccessor());
if (value == $el.val()) {
$el.iCheck('check');
} else if (value == "" || value == null) { // Handle clearing the value on reverts.
$el.iCheck('uncheck');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment