且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角 - 使用Restangular时,放弃Ajax请求

更新时间:2023-11-28 14:23:22

可以中止 $ HTTP 通过超时通话配置属性,它可以是一个承诺,即中止解决当请求。

You can abort $http calls via the timeout config property, which can be a Promise, that aborts the request when resolved.

因此​​,在restangular,你可以做到这一点像

So in restangular, you can do this like

var abort = $q.defer();
Restangular.one('foos', 12345).withHttpConfig({timeout: abort.promise}).get();
abort.resolve();

要它与你的用例整合,例如,你可以在你的服务有这样的:

To integrate it with your usecase, for example, you could have this in your service:

var abortGet;

this.getRecords = function(params) {
  ...
  if (abortGet) abortGet.resolve();
  abortGet = $q.defer();
  return Restangular
    .all('/api/records')
    .withHttpConfig({timeout: abortGet.promise})
    .post(filters);
}

这三方通话 getRecords 总是中止previous呼叫如果尚未解决!

This way calling getRecords always aborts the previous call if has not been resolved yet!

希望这有助于!