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 ',data.data);
return data.data;
}
localStorage.setItem(this.cacheKey,JSON.stringify({timeExpired:new Date().getTime()+216000000,data:data.data}));
return data.data;
},
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() && data.data.length > 0) {
console.log('CACHE');
this.parse(data);
}else{
this.inherited(arguments);
}
}else {
this.inherited(arguments);
}
}catch(e){
this.inherited(arguments);
}
}
})
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
To implement caching, I think overriding
AjaxSource
rather thanCollection
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.