Created
April 5, 2013 11:18
-
-
Save Jabher/5318554 to your computer and use it in GitHub Desktop.
Transparent caching function for AngularJS resources. Accepts factory name(string), path mask and original routes hash as arguments.
Supports callbacks in functions.
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
function createRestFactory(name, path, r){ | |
rest.factory(name, function($resource, $cacheFactory){ | |
var c=$cacheFactory(name); | |
var res = $resource(path, {}, r); | |
return (function(resource, routes, cache) { | |
var o={}; | |
angular.forEach(routes, function(value, key) { | |
o[key]=(function(r) { | |
var request=r; | |
return function(attrs) { | |
var data=cache.get(JSON.stringify({attrs: attrs, request: request})); | |
if(!(data && data.length>0)) { | |
data=resource[request].apply(this, arguments) | |
cache.put(JSON.stringify({attrs: attrs, request: request}), data) | |
} else { | |
if (typeof(arguments[1]) === typeof(function(){})){ | |
arguments[1](); | |
} | |
} | |
return data; | |
}; | |
})(key) | |
} | |
) | |
return o; | |
})(res, r, c) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment