【问题标题】:Construct new array based on lastChangedDate根据 lastChangedDate 构造新数组
【发布时间】: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


【解决方案1】:

首先,不要依赖localStorage:在Safari 的隐私模式下它是does not work,所以你需要有一些后备来防止错误。

你想要实现的是一个缓存的东西。您有两种可能性:自己做这将需要一些时间并且可能会有一些错误(我做过几次,但一点也不好玩)或使用现成的解决方案,例如这个高级angular-cache。它允许选择存储类型,可以与$http / $resource 一起使用,并允许对缓存进行操作,例如删除缓存的响应(当您获得更新的时间戳时,您需要这样做)。它非常易于使用,只需查看文档和示例即可。

【讨论】:

  • 感谢您的回复。我在适用于 Android 和 iPhone 的 Ionic 应用程序 (learn.ionicframework.com/formulas/localstorage) 中使用此代码。那么本地存储仍然是一个问题吗?它基本上是我唯一需要存储的。
  • 好吧,我真的不知道 ionic 作为浏览器使用什么......如果你使用 angular-cache 仍然不是问题,他们会关心它。
猜你喜欢
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 2021-10-19
  • 2011-04-01
  • 2011-04-01
  • 2016-08-02
  • 1970-01-01
相关资源
最近更新 更多