jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Expand documentation to include common use cases (better examples) #31

Closed jmdobry closed 11 years ago

jmdobry commented 11 years ago

Show how to use $angularCacheFactory with the $http service, for example.

CMCDragonkai commented 11 years ago

Can you provide an example how to use polyfills for the localstorage or session storage.

For example store.js is a pretty good polyfill. How to do I wrap the functionality of store.js so I can pass it into the cache factory.

I know you have said it needs setItem... etc, but there's no documentation on what the interface of those methods need to be. Such as parameters and what should return, and most importantly the actual intention of those methods.

jmdobry commented 11 years ago

angular-cache expects to find global localStorage and sessionStorage objects that implement the interface explained by the W3C Recommendation.

As far as the documentation for the interface for setItem, getItem, and removeItem, here it is:

The entire interface looks like this:

interface Storage {
  readonly attribute unsigned long length;
  DOMString? key(unsigned long index);
  getter DOMString getItem(DOMString key);
  setter creator void setItem(DOMString key, DOMString value);
  deleter void removeItem(DOMString key);
  void clear();
};

store.js appears to be a wrapper and polyfill for localStorage, exposing a completely different API to the developer. To use store.js you would need to create a proxy JavaScript Object that wraps the store.js API and exposes the standard W3C recommended API to angular-cache. For example:

var storeJsToStandard {
  getItem: store.get,
  setItem: store.set,
  removeItem: store.remove
};

$angularCacheFactory('myNewCache', {
  storageMode: 'localStorage',
  localStorageImpl: storeJsToStandard
});

EDIT: There are two ways to do polyfills: