All Projects → goodeggs → Angular Cached Resource

goodeggs / Angular Cached Resource

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

Programming Languages

coffeescript
4710 projects

Projects that are alternatives of or similar to Angular Cached Resource

Localforage
💾 Offline storage, improved. Wraps IndexedDB, WebSQL, or localStorage using a simple but powerful API.
Stars: ✭ 19,840 (+9000.92%)
Mutual labels:  offline, localstorage
sync-client
SyncProxy javascript client with support for all major embedded databases (IndexedDB, SQLite, WebSQL, LokiJS...)
Stars: ✭ 30 (-86.24%)
Mutual labels:  offline, localstorage
localForage-cn
localForage中文仓库,localForage改进了离线存储,提供简洁健壮的API,包括 IndexedDB, WebSQL, 和 localStorage。
Stars: ✭ 201 (-7.8%)
Mutual labels:  offline, localstorage
Angular Async Local Storage
Efficient local storage module for Angular apps and PWA: simple API + performance + Observables + validation
Stars: ✭ 539 (+147.25%)
Mutual labels:  offline, localstorage
Androiddevicenames
A tiny Android library that transforms the device model name into something users can understand.
Stars: ✭ 198 (-9.17%)
Mutual labels:  offline
Multiotp
multiOTP open source strong two factor authentication PHP library, OATH certified, with TOTP, HOTP, Mobile-OTP, YubiKey, SMS, QRcode provisioning, etc.
Stars: ✭ 173 (-20.64%)
Mutual labels:  offline
Multipeer
📱📲 A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices
Stars: ✭ 170 (-22.02%)
Mutual labels:  offline
React Local Storage
Showcase on how to use the native localStorage of the browser in React.
Stars: ✭ 168 (-22.94%)
Mutual labels:  localstorage
Brownies
🍫 Tastier cookies, local, session, and db storage in a tiny package. Includes subscribe() events for changes.
Stars: ✭ 2,386 (+994.5%)
Mutual labels:  localstorage
Dart
DART is a test documentation tool created by the Lockheed Martin Red Team to document and report on penetration tests, especially in isolated network environments.
Stars: ✭ 207 (-5.05%)
Mutual labels:  offline
Pwa Qr Code Scanner
Lightweight progressive web app for scanning QR codes offline
Stars: ✭ 188 (-13.76%)
Mutual labels:  offline
Magicpad
MagicPad is an encryption suite for beginners. It is designed to be run standalone via the browser or executable (Electron).
Stars: ✭ 174 (-20.18%)
Mutual labels:  offline
Timelite
Why is it 5 AM? Isn't there something simple I can use to track what I'm doing with all this time?
Stars: ✭ 201 (-7.8%)
Mutual labels:  localstorage
Rekord
A javascript REST ORM that is offline and real-time capable
Stars: ✭ 171 (-21.56%)
Mutual labels:  offline
Remember
The progressive offline Todo app
Stars: ✭ 208 (-4.59%)
Mutual labels:  offline
React Relay Offline
TypeScript library files for Relay Modern Offline
Stars: ✭ 169 (-22.48%)
Mutual labels:  offline
Apollo Offline
An offline toolkit for the Apollo client
Stars: ✭ 186 (-14.68%)
Mutual labels:  offline
Flutter localstorage
📦flutter localstorage for ios/android/desktop/web
Stars: ✭ 206 (-5.5%)
Mutual labels:  localstorage
Kiwix Desktop
Kiwix for Windows and GNU/Linux desktops
Stars: ✭ 183 (-16.06%)
Mutual labels:  offline
Remotestorage.js
⬡ JavaScript client library for integrating remoteStorage in apps
Stars: ✭ 2,155 (+888.53%)
Mutual labels:  localstorage

Cached Resource

An AngularJS module to interact with RESTful server-side data sources, even when the browser is offline. Uses HTML5 localStorage under the hood. Closely mimics the behavior of the core ngResource module, which it requires as a dependency.

Build Status npm version mit license

Features

  • Provides a simple abstraction to retrieve and save objects from a RESTful server.
  • Comes with a set of intelligent defaults that let you get started right away.
  • Caches all requests, and returns them immediately from the cache if you request them again.
  • Remmebers writes to the server, and adds them to the cache too.
  • If a write fails, try it again periodically until it succeeds. (This even works if you refresh the page in between!)
  • If you query for multiple resources in one request, each one is cached separately so you can request it from the cache individually, too.
  • Works as a drop-in replacement for Angular's $resource module.

News

It looks like this sort of functionality might be built into the upcoming Angular 2.0. Check out the design document here.


A simple example

// Register your module with ngCachedResource
angular.module('myApp', ['ngCachedResource']);

// Define a resource
var Article = $cachedResource('article', '/articles/:id', {id: "@id"});

// GET requests:
var a1 = Article.get({id: 1});
a1.$promise.then(function() {
  console.log('From cache:', a1);
});
a1.$httpPromise.then(function() {
  console.log('From server:', a1);
});

// POST/PUT/PATCH/DELETE requests:
var a2 = new Article({id: 2}); // *Note: setting the bound param (`id` in this case) is *required*
a2.title = "This article will be saved eventually...";
a2.body = "Even if the browser is offline right now.";
a2.$save();
a2.$promise.then(function() {
  console.log('Article was successfully saved.');
});

Read the tutorial on the Bites from Good Eggs blog.

*Note: Internally, $cachedResource keeps track of writes by bound params to ensure that it doesn't duplicate writes. If your bound param is null (say you're relying on the server to generate ids), then every write will replace the previous one. To avoid this undesirable scenario, simply ensure the id is set on the client, and you can safely ignore it on the server.


Installing

Bower:

bower install angular-cached-resource

npm: (intended for use with browserify)

npm install angular-cached-resource

Manual Download:


Usage

Provides a factory called $cachedResource:

$cachedResource(cacheKey, url, [paramDefaults], [actions]);

Arguments

  • cacheKey, String
    An arbitrary key that will uniquely represent this resource in localStorage. When the resource is instanciated, it will check localStorage for any

  • url, String
    Exactly matches the API for the url param of the $resource factory.

  • paramDefaults, Object, (optional)
    Exactly matches the API for the paramDefaults param of the $resource factory.

  • actions, Object, optional
    Mostly matches the API for the actions param of the $resource factory. Takes an additonal cache param (Boolean, default true) that determines if this action uses caching.

Returns

A CachedResource "class" object. This is a swap-in replacement for an object created by the $resource factory, with the following additional properties:

  • Resource.$clearCache( [options] )
    Clears all items from the cache associated with this resource. Accepts one argument, described below.

    • options, Object, optional
      options may contain the following keys:
      • where, which will limit the resources that are cleared from the cache to only those whose keys are explicitly listed. where can be an Array or an Object. If it is an Object, it will be treated like an Array containing only the provided Object. The Array should contain Objects representing cache keys that should be removed. If where is provided, exceptFor must not be provided.
      • exceptFor, which will limit the resources that are cleared from the cache to all resources except for those whose keys are explicitly listed. Just like where, exceptFor can be an Array or an Object. If it is an Object, it will be treated like an Array containing only the provided Object. The Array should contain Objects representing cache keys that should be kept. If exceptFor is provided, where must not be provided.
      • isArray, a boolean. Default is false. If true, then the function will treat the where or exceptFor arguments as referring to Array cache key.
      • clearChildren, a boolean. Default is false. If true, and isArray is also true, then the function will clear the Array cache entry (or entries) as well as all of the instances that the Array points to.
      • clearPendingWrites, a boolean. Default is false. If true, then the function will also remove cached instances that have a pending write to the server.

In addition, the following properties exist on CachedResource "instance" objects:

  • resource.$promise
    For GET requests, if anything was already in the cache, this promise is immediately resolved (still asynchronously!) even as the HTTP request continues. Otherwise, this promise is resolved when the HTTP request responds.

  • resource.$httpPromise
    For all requests, this promise is resolved as soon as the corresponding HTTP request responds.

Clearing the cache

Since there is a 5 megabyte limit on localStorage in most browsers, you'll probably want to actively manage the resource instances that are stored. By default, this module never removes cache entries, so you'll have to do this by hand. Here are the ways that you can accomplish this:

  • localStorage.clear()
    Removes everything in localStorage. This will not break the behavior of this module, except that it will prevent any pending write from actually occurring.

  • $cachedResource.clearCache()
    Removes every single Angular Cached Resource cache entry that's currently stored in localStorage. It will leave all cache entries that were not created by this module. (Note: cache entries are namespaced, so if you add anything to localStorage with a key that begins with cachedResource://, it will get deleted by this call). It will also leave any resource instances that have a pending write to the server.

  • $cachedResource.clearUndefined()
    Removes every Angular Cached Resource cache entry corresponding to a resource that has not been defined since the page was loaded. This is useful if your API changes and you want to make sure that old entries are cleared away.

  • $cachedResource.clearCache({exceptFor: ['foo', 'bar']})
    Removes every Angular Cached Resource entry except for resources with the foo or bar keys, or resource instances that have a pending write to the server.

  • $cachedResource.clearCache({clearPendingWrites: true})
    Removes every Angular Cached Resource entry, including those that have a pending write to the server.

If you have a "class" object that you've created with $cachedResource, then you can also do the following:

  • CachedResource.$clearCache()
    Removes all entries from the cache associated with this particular resource class, except for resource instances that have a pending write to the server.

  • CachedResource.$clearCache({where: [{id: 1}, {id: 2}])
    Removes two entries from the cache associated with this particular resource class; the ones with an id of 1 and 2. (This assumes that paramDefaults has an id param.)

  • CachedResource.$clearCache({exceptFor: {id: 1})
    Removes all entries from the cache associated with this particular resource class, except for those with an id of 1. (This assumes that paramDefaults has an id param.)

  • CachedResource.$clearCache({exceptFor: {query: 'search string'}, isArray: true})
    Removes all entries from the cache except those that were returned by the provided query parameters.

  • CachedResource.$clearCache({clearPendingWrites: true})
    Removes all instances of CachedResource from the cache, including those that have a pending write to the server.


Details

Asking for a cached resource with get or query will do the following:

  1. If the request has not been made previously, it will immediately return a resource object, just like usual. The request will go through to the server, and when the server responds, the resource will be saved in a localStorage cache.

  2. If the request has already been made, it will immediately return a resource object that is pre-populated from the cache. The request will still attempt to go through to the server, and if the server responds, the cache entry will be updated.

Updating a CachedResource object will do the following:

  1. Add the resource update action to a queue.
  2. Immediately attempt to flush the queue by sending all the network requests in the queue.
  3. If a queued network request succeeds, remove it from the queue and resolve the promises on the associated resources (only if the queue entry was made after the page was loaded)
  4. If the queue contains requests, attempt to flush it once per minute OR whenever the browser sends a navigator.onOnline event.

What if localStorage doesn't exist, or if the browser is out of space?

In either of these cases, $cachedResource will make sure all of your requests still happen. Things end up working just like the $resource module, with none of the caching benefits.


Development

Please make sure you run the tests, and add to them unless it's a trivial change. Here is how you can run the tests:

npm install
npm test

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].