Created
November 21, 2014 18:24
-
-
Save Fingel/5227736f6a9679c21387 to your computer and use it in GitHub Desktop.
CacheFactory for Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.factory('CacheFactory', ['$injector', function($injector){ | |
var cache = {}; | |
var cacheFactory; | |
cacheFactory = { | |
get: function(type, callback){ | |
if(!cache[type] || cache[type].length < 1){ | |
console.log("no cache"); | |
var factory = $injector.get(type); | |
factory.query({}, function(data, headers){ | |
cache[type] = data.results; | |
return callback(cache[type]); | |
}); | |
}else{ | |
console.log("cached") | |
return callback(cache[type]); | |
} | |
}, | |
clear: function(type){ | |
cache[type] = []; | |
}, | |
push: function(type, data){ | |
cache[type] = cache[type].concat(data); | |
} | |
}; | |
return cacheFactory; | |
}]) | |
CacheFactory.get('Groups', function(data){ | |
$scope.groups = data; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment