How to overide fetch collection to get data from localStorage cache?

Hi all,

In my app, i using localStore to cache data for first time call api server, but second time i cannot use data from cache to parse view. Please help me fix it.
var kind = require('enyo/kind'), AjaxSource = require('enyo/AjaxSource'), Collection = require('enyo/Collection'), Config = require('../GlobalConfig.js'); new AjaxSource({ name: 'ajax' }); module.exports = kind({ name: 'CategoryCollection', kind: Collection, options: {parse: true,fetch:true}, cacheKey:'HOMEMENU', source: 'ajax', parse: function(data) { console.log('MENU HOME',data); /** doc tu local cache */ if(typeof data.timeExpired != 'undefined' && data.timeExpired > new Date().getTime()){ console.log('MENU DATA ',; return; } localStorage.setItem(this.cacheKey,JSON.stringify({timeExpired:new Date().getTime()+216000000,})); return; }, getUrl: function() { return Config.ws_endpoint + 'menu/main'; }, fetch:function(){ try { var data = localStorage.getItem(this.cacheKey); if (data != null) { data = JSON.parse(data); if(data.timeExpired > new Date().getTime() && > 0) { console.log('CACHE'); this.parse(data); }else{ this.inherited(arguments); } }else { this.inherited(arguments); } }catch(e){ this.inherited(arguments); } } })


  • I'm not sure what is occurring based on your description. Is the data being fetched every time? How are you requesting data from your app?

    To implement caching, I think overriding AjaxSource rather than Collection would be more appropriate. The Source is the abstraction over from where a record is retrieved; Collection is an abstraction over a list of records.
Sign In or Register to comment.