I am trying to figure out where it is best to use a continuous load using Durandal.
From what I can tell, the general recommendation for loading data is in the ViewModel activate method, which I usually do - something like:
viewModel.activate = function () { var loadPromise = myService.loadData(); return $.when(loadPromise).then(function (loadedData) { viewModel.data(data); }); };
I know that if I do not return the promise here, then there are usually binding problems - as this question and answer indicate .
However, executing the long-loading operation in the activate method makes the application "frozen" when the loading operation completes. For example, what if my download was now something like this?
viewModel.activate = function () { // All loads return a promise var firstLoad = myService.loadFirstData(); var secondLoad = myService.loadSecondData(); var thirdLoad = myService.loadThirdDataWhichTakesAges(); return $.when(firstLoad, secondLoad, thirdLoad).then(function (one, two, three) { viewModel.one(one); viewModel.two(two); viewModel.three(three); }); };
In this case, the URL is updated to display the page being loaded, but the contents of the page still show the previous page (which I mean by “freezes”).
Ideally, it would be nice if the URL should change to a new page, and the page will display a new page (even if the data for this page has not yet been returned). Then, when each loading operation returns, the corresponding part of the page should be updated when the data is bound to the view model.
Is there a recommended way to achieve this inside Durandal?
My current solution is to start loading in the activate method, and then populate the data in the viewAttached method:
var loadPromise; viewModel.activate = function () {
This seems to work, but I remember reading somewhere that relying on viewAttached not a good solution. I'm also not sure if there is a chance for the race condition, since I allow the action to be activated.
Any other recommendations?