PDA

View Full Version : data corruption using LocalStorageProxy w/ Store



JacobGu
26 Jan 2012, 12:46 PM
Overview: I am seeing data corruption using LocalStorage data store on Sencha Touch 1.1.0. I am not using 1.1.1, due to other bugs in Ext.util.Stateful. Below is repro code, plus overrides I had to peform to workaround it.

If I create a model and store as follows:




App.models.MyModel = Ext.regModel('App.models.MyModel', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
]
});
App.stores.MyStore = new Ext.data.MyStore({
model: 'App.models.MyModel',
proxy: {
type: 'localstorage',
id: 'mymodels',
reader: 'json'
},

populate: function(rawItems){
// wipe out existing cache
this.remove(this.getRange());

Ext.each(rawItems, function(item, index, allItems){
this.add(item);
}, this);

this.sync();
}
});



And then I set up the data as follows:




var rawItems = [{
id: 100,
name: '100'
},{
id: 200,
name: '200'
},{
id: 300,
name: '300'
}];

App.stores.MyStore.load();
App.stores.MyStore.populate(rawItems); // ok first time if localStorage is empty
App.stores.MyStore.populate(rawItems); // second time causes corruption



This is my override workaround:



Ext.override(Ext.data.Store, {

sync: function(){
Ext.data.Store.superclass.sync.apply(this, arguments);

if(this.proxy && this.proxy.type == 'localstorage'){
this.load();
}
}
});


Ext.override(Ext.data.WebStorageProxy, {

batchOrder: 'destroy,update,create'
});