I am trying to create a kind of custom drop down which will have a method name from which it will get all of its element, and before knockout model binding it should wait for the list to be populated from the method given and then start its default behaviour.
so far I am successful on populating data from a given method. but the problem is how can I tell knockout binding to wait till my init method complete its asynch working.
ko.bindingHandlers.serviceMethod = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var serviceName, optionsValue, optionsText, value, optionsCaption, isCompleted;
if (element.nodeName == 'SELECT') {
optionsValue = allBindings().optionsValue || 'value';
optionsText = allBindings().optionsText || 'text';
serviceName = valueAccessor();
var l = $(element);
serviceName.apply().done(function (results) {
l.empty();
$.each(results.List, function (j, result) {
l.append($("<option />").val(result[optionsValue] || '').text(result[optionsText] || ''));
})
});
}
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
//what to do now in order to let knockout wait for list to be populated
}
}
my binding is <select name="state" data-bind="serviceMethod:registrationService.getAllStates,value: model.state" id="ddlState"></select>