Skip to content

Instantly share code, notes, and snippets.

@jonathancounihan
Last active August 29, 2015 14:04
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/7686fda9004ba21395a3 to your computer and use it in GitHub Desktop.
Save jonathancounihan/7686fda9004ba21395a3 to your computer and use it in GitHub Desktop.
knockout iCheck binding
<div>
<label>
<input type="checkbox" data-bind="iCheckBox: SmokerCheckBox" />
<span class="icheck-label">&nbsp;Is Smoker</span>
</label>
</div>
<div data-bind="text: ko.toJSON(vm)">
</div>
// Using ko 3.2.0 and iCheck 1.0.2
// This function is requiured for below.
ko.subscribable.fn.subscribeChanged = function (callback) {
var that = this;
if (!that.previousValueSubscription) {
that.previousValueSubscription = this.subscribe(function (_oldValue) {
that.oldValue = _oldValue;
}, that, 'beforeChange');
}
var subscription = that.subscribe(function (latestValue) {
callback(latestValue, that.oldValue);
}, that);
var protoDispose = subscription.dispose;
subscription.dispose = function () {
if (protoDispose) {
protoDispose.call(this);
}
if (that.previousValueSubscription) {
that.previousValueSubscription.dispose();
}
delete that.oldValue;
}
return subscription;
};
ko.bindingHandlers.iCheckBox = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var $el = $(element);
var observable = valueAccessor();
$el.iCheck({
checkboxClass: 'icheckbox_square-red',
inheritClass: true
});
var enabledSubs = null;
var enable = allBindingsAccessor().enable;
if (enable != undefined) {
if (enable()) {
$el.iCheck('enable');
}
else {
$el.iCheck('disable');
}
enabledSubs = enable.subscribeChanged(function (newValue, oldValue) {
if (newValue != oldValue) {
$el.iCheck('update');
}
});
}
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
if (enabledSubs != null) {
enabledSubs.dispose();
enabledSubs = null;
}
$el.iCheck('destroy');
});
// ifChecked handles tabs and clicks
$el.on('ifChecked', function (e) {
observable(true);
});
$el.on('ifUnchecked', function (e) {
observable(false);
});
ko.bindingHandlers.iCheckBox.update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
},
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 == true) {
$el.iCheck('check');
} else if (value == false || value == null || value == "") { // 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