1

I'm writting an application using angular and spring. It has to be able to work offline. After doing some research I found that application cache is the best way to go since I need to cache all the .css and .js files. The problem is that I can't get the data returned by spring and asked by the $resource object to be cached. When I turn off the server, static data are cached but I get a "GET error" in chrome's console about the .json he can't retrieve.

angular.module('MonService', ['ngResource']).
factory('Projet', function($resource){
return $resource('json/accueil');
});

I've tried something such as saving the response manually in a .json file then caching this file as well and use it as the source for the $resource but it seems long and complicated...

Or using localstorage, something like :

var cache, AmettreDansCache;
donne = {};

cache= window.localStorage.getItem('projets');

  if (!cache) {
   AmettreDansCache= $resource('json/accueil');
   window.localStorage.setItem('projets', JSON.stringify(AmettreDansCache));
   return AmettreDansCache
  }
 else{
    return angular.extend(donne, JSON.parse(cache));
 }

i don't think this is working, anyway what's the way to do it using application cache only ?

3

1 Answer 1

2

There is a module built on top of resource that does caching, on both reading and writing data. You should check it out. It will keep a copy of your data on the clients browser and when failed to save (due to being offline) it will save it locally and keep retrying to save.

https://github.com/goodeggs/angular-cached-resource

There is also a small article about the module:

http://bites.goodeggs.com/open_source/angular-cached-resource/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.