Question: How can I structure my code so that knockout bindings are not applied until all requests for the ViewModel are completed?
Update:. After some further research and experimentation, I think that using something in accordance with the snooze function may work. I tried several implementations, however, it only postpones the request until the request is called, and not until all the results of the request have been processed. I obviously am doing something wrong, but my javascript foo is weak.
Used technologies: Entity Framework 5 w / Oracle, .Net 4 Web API, knockout 2.2, breeze 0.71.3
Situation: A breeze is used to invoke a Web API method that extracts an Enumerable from POCOs, populates the observable array with a knockout, and the array is bound to a select control in the view.
Problem: Breeze requests were not completed, and observed knockouts were not filled before the ViewModel bindings on the view were applied. When the query results are returned, the user interface does not respond for 5-7 seconds, and the ko-observable is populated, and thus the selection control is updated. Based on the registration, this seems to be the problem ...
Cshtml file:
<select data-bind="options: $root.brokers, value: 'id', optionsText: 'name'"> <script data-main="/BrokerCommission/Scripts/app/main" src="/BrokerCommission/Scripts/require.js"></script>
main.js:
requirejs.config( { // well-know paths to selected scripts paths: { 'breeze': '../breeze.debug', // debug version of breeze 'text': '../text'// html loader plugin; see http://requirejs.org/docs/api.html#text } } ); define(['logger', 'text', 'breeze'], function(logger) { require(['vm.muni'], function() { logger.info('applying bindings'); ko.applyBindings(my.vm); });
vm.muni is my javascript ViewModel file. The method that executes the request is called here:
getAllBrokers = function () { dataservice.getBrokers() .then(processBrokerQueryResults) .fail(handleQueryErrors); }, processBrokerQueryResults = function (data) { logger.info("Start loading Brokers " + Math.round(new Date().getTime() / 1000)); my.vm.brokers([]); $.each(data.results, function (i, d) { brokers.push(new my.Broker() .id(d.id) .name(d.name) ); }); logger.info("End loading Brokers " + Math.round(new Date().getTime() / 1000)); },
Here is the breeze request from dataservice.js file:
function getBrokers() { var query = new entityModel.EntityQuery() .from("GetBrokers")