Knockout Custom Binding For Bootstrap Select Not Updating Viewmodel "selectedcategories/selectedcategory"
I am creating a knockout js custom binding that will allow me to use data-binding with bootstrap multiselect. I need it to work for single
Solution 1:
I'm not sure I can get you to the point of making it work, but some pointers:
- DOM manipulation belongs in the binding handler. In particular, the initialization:
$(this).multiselect({
goes in theinit
. - The parameter object you pass to that initialization is what should be the bound variable for your
multiselect
binding. (I have no idea what_categoryID
is for.) It will be what causes theupdate
to fire. It will be whatvalueAccessor()
returns. - Although
optionsText
andoptionsValue
should have simple string arguments,value
's argument should be an unquoted observable name. This is one key issue in the question you're asking. - Only wrap (directly call the
ko.bindingHandlers
entry for) handlers that you aren't binding separately, and only when you need their binding to be part of what you're doing. You may need to wrapoptions
,optionsText
, andoptionsValue
instead of including them in thedata-bind
. But one or the other.
Update I've updated your fiddle to handle the multiselect case. I didn't find that the selectedOptions
binding handler was working for me, so I had to put this in the init
. This breaks the single-select case; need to special-case it.
$(element).change(function (e) {
var selectedOptions = ko.utils.arrayFilter(Array.from(e.target.options), function (opt) {
return opt.selected;
});
var selectedValues = ko.utils.arrayMap(selectedOptions, function (opt) {
return opt.value;
});
valueAccessor()(selectedValues);
});
Post a Comment for "Knockout Custom Binding For Bootstrap Select Not Updating Viewmodel "selectedcategories/selectedcategory""