我有一个ViewModel" AppViewModel
"获取JSON
数据来创建模型,其中一个属性为" totalSessions
"是使用ajax
电话提取的,"模型" AppViewModel
返回一个可观察的数组,我的代码正在运行,没有任何错误,但视图似乎没有更新。
var jsonapparray = [];
function AppViewModel(appsdata)
{
var self = this;
var appsdata = $.parseJSON("["+ JSON.stringify(appsdata["user_of"]) + "]");
var totalSessions = 0;
var i = 0;
for (var appkey in appsdata[0]) {
console.log(appsdata[0][appkey].name);
var elem = new Object();
elem._id = appsdata[0][appkey]._id;
elem.category = appsdata[0][appkey].category;
elem.country = appsdata[0][appkey].country;
elem.name = appsdata[0][appkey].name;
elem.key = appsdata[0][appkey].key;
elem.timezone = appsdata[0][appkey].timezone;
elem.totalsessions = 000;
jsonapparray.push(elem);
updateSessionsInfo(i,elem,jsonapparray);
i++;
}
self.AppCount = i;
self.Apps = ko.observableArray(jsonapparray);
function updateSessionsInfo(i,elem,jsonapparray)
{
$.ajax({
type:"GET",
url:Domain + "/o",
data:{
"api_key":readCookie("api_key"),
"app_id":elem._id,
"method":"sessions"
},
dataType:"jsonp",
async: false,
success:function (json) {
_sessionDb = json;
var totalSessions = _.pluck(_sessionDb,"t");
jsonapparray[j].totalsessions = totalSessions[0];
}
});
}
}
答案 0 :(得分:0)
你应该改变:
jsonapparray[j].totalsessions = totalSessions[0];
为:
jsonapparray[i].totalsessions = totalSessions[0];
但这实际上并没有帮助。更新阵列中项目的属性不会触发更新。您应该可以观察所有属性,例如:
elem.totalsessions = ko.observable(0);
然后像这样更新值:
jsonapparray[i].totalsessions(totalsessions[0]);