Created
June 25, 2014 09:42
-
-
Save jonathancounihan/ffbbe2d16dd02cfdf984 to your computer and use it in GitHub Desktop.
knockout binding for iCheck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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