javascript - 异步 $cacheFactory 与 $http 和 $resource

标签 javascript angularjs http cachefactory

我正在尝试使用 $cacheFactory 在 $resource 之上实现异步缓存。

在 $http 源码中查找它似乎支持从缓存中返回 promise 。我会假设在那之后我可以简单地用缓存数据解决 promise 或拒绝它并让 $http 做它的事情,然后将数据放回缓存中。问题是.. 我只是不工作。 $http 实际上支持 promises 吗?

https://github.com/angular/angular.js/blob/master/src/ng/http.js#L895

  if (cache) {
    cachedResp = cache.get(url);
    if (isDefined(cachedResp)) {
      if (isPromiseLike(cachedResp)) {
        // cached request has already been sent, but there is no response yet
        cachedResp.then(removePendingReq, removePendingReq);
        return cachedResp;
      } else {
        // serving from cache
        if (isArray(cachedResp)) {
          resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3]);
        } else {
          resolvePromise(cachedResp, 200, {}, 'OK');
        }
      }
    } else {
      // put the promise for the non-transformed response into cache as a placeholder
      cache.put(url, promise);
    }
  }

这是 $http 处理缓存的地方,正如您所看到的,它确实检查了 promise 是否正在返回(第 898 行)。但似乎解决或拒绝都只是从待处理队列中清除请求。我如何实际发送数据或单个 $http 以继续请求?

这是关于我正在努力完成的事情的 Plunker。

http://plnkr.co/edit/TwXumrAunG9b5JKo5OlB?p=preview

最佳答案

AngularJS 中有一个错误。当解析(或拒绝)来自缓存的 promise 返回时,$http 不会以任何方式处理数据。

相关错误 https://github.com/angular/angular.js/pull/6534

关于javascript - 异步 $cacheFactory 与 $http 和 $resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24920900/

相关文章:

javascript - 按箭头键时光标会跳跃

javascript - 使用键/值的 Angular JS 顺序选择

javascript - Angular - 获取$scope中某个键的值

javascript - AngularJS : Why to check whether newValue is the same as oldValue in `$watch` listener?

angularjs - 为什么元素在指令的链接功能中不可用

python - 使用 Bottle 框架在 Python 中通过 http 发送文件

http - Web 套接字协议(protocol)握手与交换协议(protocol)

javascript - 想要在 Div 中加载数据后执行脚本

php - 在 PHP 中处理 cURL

javascript - 在 YDN-DB 中搜索多个值