How can I implement caching in the angularJs factory without using cacheService?
Below is the implementation of caching in angularjs without using cachService:
angular.module('as.admin')
.factory('userAnalyticsData',userAnalyticsData);
userAnalyticsData.$inject = ['$http'];
function userAnalyticsData($http) {
var service = {
getData: getData
};
var analyticsData = {};
var called=false;
var unCalledCallbacks=[];
return service;
function getData(data,callback) {
if(!data){
if(callback) callback(false); return;
}
if(called==true){
if(!angular.equals(analyticsData, {}) && unCalledCallbacks.length==0){
if(callback)callback(analyticsData); return;
}
if(callback){
unCalledCallbacks.push(callback);
}
}
if(!called){
called=true;
$http({
method: 'POST',
url : './admin/analytics',
data:data
}).then(function(res){
if(res && res.data && res.data.success==1){
analyticsData=res.data;
if(callback) callback(analyticsData);
if(unCalledCallbacks){
var i=0;
while (i < unCalledCallbacks.length) {
if (unCalledCallbacks[i]) {
unCalledCallbacks[i](analyticsData);
unCalledCallbacks.splice(i, 1);
} else {
++i;
}
}
}
}
else{
if(callback) callback(false);
}
});
}
}
}
})();