【发布时间】:2015-12-31 11:24:26
【问题描述】:
我有一个 refreshdata() 函数,它执行调用以获取某些事件的 ID:
function refreshdata()
{
$http({
url: “…/GetIDs”,
method: "POST",
data: jsonData,
dataType: "json",
timeout: 7000,
contentType: 'application/json; charset=utf-8'
}).success(function(data, status, headers, config) {
responseNew=data.d;
meetings = JSON.parse(responseNew);
console.log("Got meetings");
meetingsanddetails(meetings);
}).error(function(data, status, headers, config) {
alert("Unable to refresh data.”);
});
}
meetings 类似于:
{
"Result":"success",
"Key":"12345",
"Data":[
{"ID":"GFDCV34","lastChangedDate":"2015-12-03 11:14:27"},
{"ID":"IDJHE23","lastChangedDate":"2015-12-03 15:17:47"},
{"ID":"KDJBD34","lastChangedDate":"2015-12-03 05:25:11"}
]
}
接下来,我做meetingsanddetails(meetings):
res = meetings;
var i = 0;
var tmp = [];
promises = [];
res.Data.map(function (val) {
promises.push(getdetails2(val.ID).then(function (data) {
tmp = JSON.parse(data);
Object.keys(tmp.Data[0]).map(function (v, j) {
val[v] = tmp.Data[0][v];
});
}, function (error) {
console.log(error)
}));
});
$q.all(promises).then(function () {
$timeout(function () {$ionicLoading.hide()}, 500);
$window.localStorage['data'] = JSON.stringify(res);
});
res 现在类似于:
{
"Result": "success",
"Key": "12345",
"Data":[
{
"ID": "GFDCV34",
"name": "Name of event 1",
"date": "date of event 1",
"location": "location of event 1",
"lastChangedDate": "2015-12-03 11:14:27"
},
{
"ID": "IDJHE23",
"name": "Name of event 2",
"date": "date of event 2",
"location": "location of event 2",
"lastChangedDate": "2015-12-03 15:17:47"
},
{
"ID": "KDJBD34",
"name": "Name of event 3",
"date": "date of event 3",
"location": "location of event 3",
"lastChangedDate":"2015-12-03 05:25:11"
}
]
}
(感谢maurycy)
(信息:getdetails2(id) 返回具有给定 ID 的事件的详细信息)。
这工作正常,但 getdetails2 调用需要很长时间才能加载。这就是我想让它像这样工作的方式:
如果 …/GetIDs 返回的 ID 不在 $window.localStorage['data'] 中,则应添加它们的详细信息。
…/GetIDs 返回的 ID 已经在 $window.localStorage['data’] 中,只有在新调用的 lastChangedDate 更新时才应该更新。
应删除 $window.localStorage['data’] 中存在但新呼叫中不存在的 ID。
谁能指出我正确的方向?我一直在搞砸这个数组恶作剧。
【问题讨论】:
-
在
res.Data.map使用forEach而不是map在逻辑上更正确
标签: javascript arrays angularjs