restangular - capturing full URL of a GET request in AngularJS including query params -
i'm using http interceptor. see values when make restangular request. here code request interceptor.
request: function (config) { // see link below see value of config console.log('config', config); // below returns full url except query string console.log('location', $location.absurl()); // $rootscope.cachedata $cachefactory console.log('$rootscope', $rootscope.cachedata); // returns {id: "http", size: 3} console.log('cache info', $rootscope.cachedata.info()); // below returns undefined console.log('cache get', $rootscope.cachedata.get('http')); // other codes removed since it's not related // ........ // ........ // return config or wrap in promise if blank. return config || $q.when(config); },
config value : http://i.imgur.com/l0isxbj.png
unfortunately, preparing params captured manually not 100% guaranteed match has been cached. noticed cachefactory checks exact string requested. if our request's query parameters age=12&name=scott on our http interceptor, prepare other way putting name first age(name=scott&age=12), cachefactory won't find it.
so i'm trying angular service or factory return full url equal request made. tried $location it's not giving full request.
i decided parse config , build scratch. it's working great :)
if ( config.method == 'get' && (config.url.indexof('v1/repeters') != -1) ) { // prepare full path var cachedurlslocalstorage; var absolutepath = ''; _(config.params).keys().sort().each( function(key, index) { var value = config.params[key]; absolutepath = absolutepath + key + '=' + value + (index < _(config.params).keys().value().length - 1 ? '&' : ''); }); cachedurlslocalstorage = json.parse(window.localstorage.getitem('cachedurls')); if (cachedurlslocalstorage) { var exists = _.findindex(cachedurlslocalstorage, function(cacheddata) { return cacheddata.url == config.url + '?' + absolutepath; }); if (!exists) { cachedurlslocalstorage.push({url : config.url + '?' + absolutepath, timeexecuted : moment(), expirytime : moment().add(10, 'minutes')}); window.localstorage.setitem('cachedurls', json.stringify( cachedurlslocalstorage )); } } else { cachedurlslocalstorage = []; cachedurlslocalstorage.push({url : config.url + '?' + absolutepath, timeexecuted : moment(), expirytime : moment().add(10, 'minutes')}); window.localstorage.setitem('cachedurls', json.stringify( cachedurlslocalstorage )); } }
Comments
Post a Comment