Skip to content

Instantly share code, notes, and snippets.

@mcarpenterjr
Forked from tomazy/knockout-chosen.js
Created October 6, 2017 14:11
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 mcarpenterjr/ed5f2eeaacedf6a4186d0b7eeb06c6a3 to your computer and use it in GitHub Desktop.
Save mcarpenterjr/ed5f2eeaacedf6a4186d0b7eeb06c6a3 to your computer and use it in GitHub Desktop.
knockout + jquery chosen binding handler http://jsfiddle.net/tomazy/6A57J/
ko.bindingHandlers.chosen = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext){
var $element = $(element);
var options = ko.unwrap(valueAccessor());
if (typeof options === 'object')
$element.chosen(options);
else
$element.chosen();
['options', 'selectedOptions', 'value'].forEach(function(propName){
if (allBindings.has(propName)){
var prop = allBindings.get(propName);
if (ko.isObservable(prop)){
prop.subscribe(function(){
$element.trigger('chosen:updated');
});
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment