Reputation: 3590
I am trying to make a simple KO custom bindings wrapper for the "options" binding similar to what is described in this example. My goal is to have a custom binding that will apply select2.js
to the specified select box.
I am trying to get started by just wrapping the options binding in a custom wrapper, but for some reason it is not working.
Here is what I have (jsFiddle) :
ko.bindingHandlers.select2 = {
init: function (element) {
ko.bindingHandlers.options.init(element);
},
update: function (element, valueAccessor, allBindingsAccessor) {
ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor);
}
};
Any help on this would be greatly appreciated.
Upvotes: 2
Views: 2806
Reputation: 114792
Looks like your issue is just related to the way that jsFiddle is loading the scripts. You had it set to onLoad
, which was causing your applyBindings to be called prior to your creation of the custom binding.
If you change the fiddle to use something like No wrap in <body>
it will work, except for one minor issue:
The options
binding does not have an init
binding in version 2.2 and below. It will have an init
function in 2.3 and beyond. If you don't need to do anything further in your init
function (strictly wrapping it), then you can do:
init: ko.bindingHandlers.options.init,
update: function (element, valueAccessor, allBindingsAccessor) {
ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor);
}
It will either be undefined or use what is there (for 2.3+).
Sample: http://jsfiddle.net/rniemeyer/AerJ5/
Upvotes: 2