我创建了一个UserService,如下所示:
angular.module('nrApp').factory('userService',['Restangular','usermodel','DSCacheFactory',function (Restangular,usermodel,DSCacheFactory) { // Create a new cache called "profileCache" var userCache = DSCacheFactory('userCache',{ maxAge: 3600000,deleteOnExpire: 'aggressive',storageMode: 'localStorage',// This cache will sync itself with `localStorage`. onExpire: function (key,value) { Restangular.oneUrl('users',key).get().then(function(data) { userCache.put(key,data); }); } }); Restangular.extendModel('users',function(obj) { return usermodel.mixInto(obj); }); Restangular.addRequestInterceptor(function(element,operation,what,url) { if(operation === 'get') { debugger; //Check the cache to see if the resource is already cached var data = userCache.get(url); //If cache object does exist,return it if(data !== undefined) { angular.extend(element,data); } return element; } }); Restangular.addResponseInterceptor(function(data,url,response) { //Cache the response from a get method if(operation === 'get') { debugger; userCache.put(url,data); } //Unvalidate the cache when a 'put','post' and 'delete' is performed to update the cached version. if (operation === 'put' || operation === 'post' || operation === 'delete') { userCache.destroy(); } return response; }); return Restangular.service('users'); }]);
从注释中可以看出,我想要实现的是每当使用Restangular通过此服务执行Get请求时,都会检查本地缓存,如果缓存返回一个对象,它将扩展到restangular元素.想要实现的流程是在为该请求找到缓存对象时取消对服务器的请求.
但是没有任何运气,即使在缓存中找到了对象,addResponseInterceptor方法仍然会执行.
在“获取”请求期间是否有任何可能的解决方案来取消对服务器的请求?
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。