Last active
June 13, 2016 21:37
-
-
Save ssajous/5823594 to your computer and use it in GitHub Desktop.
Knockout.js binding handler for the JQuery UI multiselect widget
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
<select multiple="multiple" | |
data-bind="multiselect : { config : multiSelectConfig, | |
options: options, | |
selectedOptions : selectedOptions }" /> |
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.multiselect = { | |
init: function (element, valueAccessor, allBindingAccessors) { | |
"use strict"; | |
var options = valueAccessor(); | |
ko.bindingHandlers.options.update( | |
element, | |
function() { return options.options; }, | |
allBindingAccessors | |
); | |
ko.bindingHandlers.selectedOptions.init( | |
element, | |
function() { return options.selectedOptions; }, | |
allBindingAccessors | |
); | |
ko.bindingHandlers.selectedOptions.update( | |
element, | |
function() { return options.selectedOptions; }, | |
allBindingAccessors | |
); | |
$(element).multiselect(options.config); | |
//make view model know about select/deselect changes | |
$(element).bind("multiselectcheckall", function () { $(element).trigger("change"); }); | |
$(element).bind("multiselectuncheckall", function () { $(element).trigger("change"); }); | |
}, | |
update: function (element, valueAccessor, allBindingAccessors) { | |
"use strict"; | |
var options = valueAccessor(); | |
//update html if items set through code | |
ko.bindingHandlers.selectedOptions.update( | |
element, | |
function () { return options.selectedOptions; }, | |
allBindingAccessors | |
); | |
$(element).multiselect("refresh"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment