UNPKG

angular-cached-resource

Version:

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

67 lines (58 loc) 2.23 kB
// Generated by CoffeeScript 1.7.1 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; })(); };