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.
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
optionsbinding does not have aninitbinding in version 2.2 and below. It will have aninitfunction in 2.3 and beyond. If you don't need to do anything further in yourinitfunction (strictly wrapping it), then you can do:It will either be undefined or use what is there (for 2.3+).
Sample: http://jsfiddle.net/rniemeyer/AerJ5/