aahung aahung - 3 months ago 31
Javascript Question

AngularJS, save resource service json to localstorage and update conditionally

I need to update server-sided data(large) only when new version detected, I am using ngResource,

goodServices.factory('Good', ['$resource',
return $resource('../WebService/list_goods.ashx?gid=:gid', {}, {
query: {method:'GET', params:{q:'all'}, isArray:true}

how to save it to LocalStorage and every time detect server (GET a version number), when new version detected, it will refresh LocalStorage.


for communicating with local storage I want to suggest ngStorage you can inject it in your controller

function yourBaseController(Good,$localStorage){
   var newVersion = Good.get(..your params), // it should return your new version
       currentVersion = $localStorage.version;
   if(newVersion !== currentVersion ){
      $localStorage.version = newVersion;

Or if you don't have a base controller which is working at all time when your app bootstraps, you can move this logic in .run block