我遇到了一些奇怪的绑定/计时行为 - 我已经粘贴了相关代码。当我在 chrome 中手动缓慢地逐步执行代码时,当我让它正常运行时,它会正确填充我的下拉列表,但它不会....我做错了什么?我已经输入了控制台输出,其中我的“获得省份”的时间不同。
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");
}
简单的 ajax 调用如下,我传入我的 observable(这几乎完全来自 John Papa 的复数演示):
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');
}
},
两者的控制台输出不同如下。
当我将断点放入并手动执行时,我得到以下信息并且它运行良好:
["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
当我删除断点并让它运行时,输出如下所示,并且没有填充任何内容。
["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