A typical scenario in our environment is to allow the user to select a list of options provided by the server (terminals, products, ..), and then present the requested data.
The options provided by the server is in Name, ID format, and thus the following knockout construct is used all to often:
<select data-bind="options: serverOptions, optionsText: 'Name', optionsValue: 'ID', value: selectedOption>
It would be desirable to make a custom bindingHandler, called 'NamedIdOptions' specifying optionsText and optionsValue on the allBindingsAccessor() and then redirect to the standard options binding handler.
i.e.
<select data-bind="NamedIdOptions: serverOptions, value: selectedOption"></select>
Previously, I have made own binding handler which fills in the options my self - however, I would prefer to use the framework provided by the options binding handler.
I have tried different approaches without too much success - the options binding uses allBindings['optionsValue'], and allBindings['optionsText'] to access the value, and it seems that I have no way of setting these. (I would like to avoid applyBindingsToNode approach used in and write something along the lines of:
ko.bindingHandlers.NamedIdOptions = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel)
{
var allBindings = allBindingsAccessor();
allBindings.*FORCESET*("optionsText", "Name");
allBindings.*FORCESET*("optionsValue", "ID");
retun ko.bindingHandlers.options.init.apply(this, arguments);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel)
{
retun ko.bindingHandlers.options.update.apply(this, arguments);
}
}
However, it seems like I have no possiblity to set anything on the allBindings.
I am not allowed to use
allBindings['_ko_property_writers']['optionsText']("Name" );
allBindings['_ko_property_writers']['optionsValue']("ID" );
I would really prefer to avoid applyBindingsToNode in the init construct as
Knockout - is it possible to combine standard select bindings with a custom binding?
Does anyone now about a simple solution to the problem?