Skip to content

Instantly share code, notes, and snippets.

@louy
Created July 10, 2014 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louy/0afbda9d04a1647d9eb2 to your computer and use it in GitHub Desktop.
Save louy/0afbda9d04a1647d9eb2 to your computer and use it in GitHub Desktop.
Knockout Chosen Binding Handler with Cleanup (jQuery)
ko.bindingHandlers.chosen = {
listenBindings: ['value', 'disable', 'options', 'foreach'],
init: function( element, valueAccessor, allBindings ) {
var options = ko.unwrap(valueAccessor()), $_ = $(element);
$_.chosen( $.extend( options, {
width: '100%',
} ) );
ko.computed(function() {
$.each(ko.bindingHandlers.chosen.listenBindings, function( i, binding ) {
var b = allBindings.get(binding);
b = $.isFunction(b) ? b() : b;
ko.unwrap(b);
$_.trigger('chosen:updated');
} );
}, null, { disposeWhenNodeIsRemoved: element });
ko.utils.domNodeDisposal.addDisposeCallback(element, function(node) {
$(node).chosen('destroy');
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment