Last active
October 6, 2017 14:11
-
-
Save tomazy/9800573 to your computer and use it in GitHub Desktop.
knockout + jquery chosen binding handler http://jsfiddle.net/tomazy/6A57J/
This file contains hidden or 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 = { | |
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
For some reason I had to add some setTimeout to the
$element.trigger('chosen:updated')
to get this gist working.