Durandal knockout - weird binding / temporary bug?

I experience some weird binding / time behavior - I inserted the appropriate code. When I slowly go through the code manually in chrome, it automatically fills in my snapshot when I let it work fine, but that’s not ... what am I doing wrong? I set the console output, where the time of my "received the province" is different.

var provinces = ko.observableArray(); var vm = { activate: activate, viewAttached:viewAttached, title: 'Home View', provinces: provinces, }; return vm; function activate() { getProvinces(provinces); return true; } function viewAttached() { console.log("got data"); } 

A simple ajax call is as follows where I go in my observable (this is taken almost exactly from the John Papa plural demodulation):

 getProvinces: function (observableArray) { observableArray([]); var options = { url: currentServer + 'regions/GetInUseProvinces', type: 'GET', async: true, dataType: "json", }; $.ajax(options).then(querySucceded); function querySucceded(data) { var provinces = []; data.forEach(function(item) { var s = new MedappData.Data.Province(item.Name, item.ID); provinces.push(s); }); observableArray(provinces); console.log('got provinces'); } }, 

The console output is different from two.

When I put breakpoints and execute it manually, I get the following and it works fine:

 ["Activating", Object] ["[viewmodels/shell] ", "Medapp loaded..."] system.js:62 ["Activating Route", Object, Object, n.Object] ["Activating", Object] ["Binding", "views/shell", Object] got provinces vendor:5319 ["Binding", "views/nav", ko.bindingContext] ["Binding", "views/home", Object] ["Binding", "views/footer", ko.bindingContext] got data 

When I delete my breakpoints and let it run the output, follow it and nothing fills.

 ["Activating", Object] ["[viewmodels/shell] ", "Medapp loaded..."] system.js:62 ["Activating Route", Object, Object, n.Object] ["Activating", Object] ["Binding", "views/shell", Object] ["Binding", "views/nav", ko.bindingContext] ["Binding", "views/home", Object] ["Binding", "views/footer", ko.bindingContext] got provinces vendor:5319 got data 
+1
source share
1 answer

getProvinces is an asynchronous operation, so you need to return the promise to let Durandal figure out when he is allowed to continue composing. Returning true after the async operation does not do this.

$.ajax(... already returns a promise, so changing getProvinces to

 return $.ajax(options).then(querySucceded); 

and in activate

 function activate() { return getProvinces(provinces); } 

gotta do the trick.

+3
source

Source: https://habr.com/ru/post/950598/


All Articles