UNPKG

angular-cached-resource

Version:

An AngularJS module to interact with RESTful resources, even when browser is offline

67 lines (58 loc) 2.2 kB
// Generated by CoffeeScript 1.10.0 var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; module.exports = function(providerParams) { var Cache, CachedResourceManager, buildCachedResourceClass; buildCachedResourceClass = require('./build_cached_resource_class'); Cache = require('./cache')(providerParams); return CachedResourceManager = (function() { function CachedResourceManager($resource, $timeout, $q) { this.byKey = {}; this.build = angular.bind(this, buildCachedResourceClass, $resource, $timeout, $q, providerParams); } CachedResourceManager.prototype.keys = function() { return Object.keys(this.byKey); }; CachedResourceManager.prototype.add = function() { var CachedResource, args; args = Array.prototype.slice.call(arguments); CachedResource = this.build(args); this.byKey[CachedResource.$key] = CachedResource; CachedResource.$writes.flush(); return CachedResource; }; CachedResourceManager.prototype.flushQueues = function() { var CachedResource, key, ref, results; ref = this.byKey; results = []; for (key in ref) { CachedResource = ref[key]; results.push(CachedResource.$writes.flush()); } return results; }; CachedResourceManager.prototype.clearCache = function(arg) { var CachedResource, clearPendingWrites, exceptFor, key, ref, ref1, results; ref = arg != null ? arg : {}, exceptFor = ref.exceptFor, clearPendingWrites = ref.clearPendingWrites; if (exceptFor == null) { exceptFor = []; } ref1 = this.byKey; results = []; for (key in ref1) { CachedResource = ref1[key]; if (indexOf.call(exceptFor, key) < 0) { results.push(CachedResource.$clearCache({ clearPendingWrites: clearPendingWrites })); } } return results; }; CachedResourceManager.prototype.clearUndefined = function() { return Cache.clear({ exceptFor: this.keys() }); }; return CachedResourceManager; })(); };