Commit 925c34d4 authored by Luís Rudge's avatar Luís Rudge

rewriting the binding handler

we dont need to call $('#element').multiselect() before ko anymore
parent cb4d0abd
...@@ -22,26 +22,18 @@ ...@@ -22,26 +22,18 @@
if(typeof ko != 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect){ if(typeof ko != 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect){
ko.bindingHandlers.multiselect = { ko.bindingHandlers.multiselect = {
init: function (element) { init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var ms = $(element).data('multiselect'); var multiSelectData = valueAccessor();
var options = ko.utils.unwrapObservable(multiSelectData.options);
if(!ms) var optionsText = allBindingsAccessor().optionsText;
throw new Error("Bootstrap-multiselect's multiselect() has to be called on element before applying the Knockout View model!"); var optionsValue = allBindingsAccessor().optionsValue;
var prev = ms.options.onChange; ko.applyBindingsToNode(element, { options: options, optionsValue: optionsValue, optionsText: optionsText }, viewModel);
$(element).multiselect(ko.utils.unwrapObservable(multiSelectData.initOptions));
ms.options.onChange = function(option, checked){ var ms = $(element).data('multiselect');
// We dont want to refresh the multiselect since it would delete / recreate all items console.log(ms);
$(element).data('blockRefresh', true); },
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// Force the binding to be updated by triggering the change event on the select element
$(element).trigger('change');
// Call any defined change handler
return prev(option, checked);
}
},
update: function (element) {
var blockRefresh = $(element).data('blockRefresh') || false; var blockRefresh = $(element).data('blockRefresh') || false;
if (!blockRefresh) { $(element).multiselect("rebuild"); } if (!blockRefresh) { $(element).multiselect("rebuild"); }
$.data(element, 'blockRefresh', false); $.data(element, 'blockRefresh', false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment