Skip to content

Cant cache into local storage. #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Nov 17, 2014 · 7 comments
Closed

Cant cache into local storage. #244

ghost opened this issue Nov 17, 2014 · 7 comments

Comments

@ghost
Copy link

ghost commented Nov 17, 2014

Hi I'm having trouble configuring my resources to cache into local storage.

I've tried defining the following in app config:

DSCacheFactoryProvider.setCacheDefaults({
storageMode: 'localStorage'
});

I've also tried passing the 'storageMethod' option when defining a resource as follows:

DS.defineResource({
name: 'someResource',
idAttribute: 'id',
storageMode: 'localStorage',
endpoint: '/fixtures',
});

all to no avail, I've read the documentation end to end and have found little information on the subject, I've also read the source for angular-data and could only find one reference to storage mode on
line 5640: storageMode: 'memory'... please could you advise on the best solution to this problem? I was thinking of using a third party API to handle local storage, but would much prefer to use angular-data exclusively.

thanks for the great tool by the way!

Dom

@jmdobry
Copy link
Member

jmdobry commented Nov 17, 2014

Angular-data doesn't use the cache factory for storing data in localStorage, rather you need to use DSLocalStorageAdapter.

angular.module('myApp', ['angular-data.DS']).config(function (DSProvider) {
  // use the local storage adapter by default
  DSProvider.defaults.defaultAdapter = 'DSLocalStorageAdapter';
}).run(function (DS) {
  // or a single resource will use it by default
  DS.defineResource({
    name: 'user',
    defaultAdapter: 'DSLocalStorageAdapter'
  });

  // or just use it for a single call
  DS.find('user', 1, { adapter: 'DSLocalStorageAdapter' })
});

See also:

@jmdobry jmdobry closed this as completed Nov 17, 2014
@ghost
Copy link
Author

ghost commented Nov 18, 2014

ok so is there no way for all http requests I make to be cached to local storage automatically by angular-cache?

@jmdobry
Copy link
Member

jmdobry commented Nov 18, 2014

There is:

angular.module('myApp', ['angular-data.DSCacheFactory']).run(function ($http) {
  $http.defaults.cache = DSCacheFactory('myCache', {
    storageMode: 'localStorage'
  });
});

@ghost
Copy link
Author

ghost commented Nov 18, 2014

perfect thankyou very much!

@splatcat
Copy link

splatcat commented Dec 1, 2014

Hi Jason, sorry for the duplicate post. Although I think I am trying to do something slightly different.

I am trying to have my app working and usable offline. So I would inject an object into the angular-data datastore. I would then update that object as and when I choose. (all the time the object, and others like it are syncing with localStorage) then when I am online or attempt a async method, I post that object to the server and update my angular data store with the result of that store.

Essentially, the client holds the most current data, this data is always synced with localstorage with all synchronous methods. When we do an async method we update the server and client with newly created IDs and potentially other data attributes.

@jmdobry
Copy link
Member

jmdobry commented Dec 1, 2014

The only way the data store can interact with localStorage is through DSLocalStorageAdapter. Right now there are no auto-sync features. As far as angular-data is concerned, localStorage is a persistence layer, and the data store can only interact with persistence layers through adapters. Synchronous methods operate on the in-memory data store only. Only the async methods work through the adapters.

@splatcat
Copy link

splatcat commented Dec 4, 2014

Hi Jason,

Thank you for your help with angular data. Again love the work you have
done. We are working on syncing these resources in our project. If you are
interested I could keep you updated :)

All the best and maybe talk soon,
Stuart

On 1 December 2014 at 16:26, Jason Dobry [email protected] wrote:

The only way the data store can interact with localStorage is through
DSLocalStorageAdapter. Right now there are no auto-sync features. As far as
angular-data is concerned, localStorage is a persistence layer, and the
data store can only interact with persistence layers through adapters.
Synchronous methods operate on the in-memory data store only. Only the
async methods work through the adapters.


Reply to this email directly or view it on GitHub
#244 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants