Created
July 10, 2014 02:42
-
-
Save louy/0afbda9d04a1647d9eb2 to your computer and use it in GitHub Desktop.
Knockout Chosen Binding Handler with Cleanup (jQuery)
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.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