Reputation: 2201
The Local Storage proxy seems to be rather buggy in Extjs 4.1. Please tell me I'm doing something wrong.
I'm attempting (like others) to load data from the server then store it locally for offline use. My problem involves refreshing the data (hitting reload in the browser). removeAll()
doesn't remove any records. I've read that you can clear local storage out by calling clear()
on the local storage proxy itself. While the records are removed, the key generator isn't reset. I'm concerned this will eventually cause a problem.
In my code, what am I doing that would cause LocalStorage to misbehave? I know there is some trickiness with the setting id's correctly on the model, but I think I'm doing that correctly.
Here's the model and store:
Ext.define('MAP.model.LookupData', {
extend: 'Ext.data.Model',
idProperty:'localId',
fields: [
'localId', // used for storing data via a localstorage proxy. -jg
'name',
'value'
]
});
Ext.define('MAP.store.LocalCloudConditionStore', {
extend: 'Ext.data.Store',
model: 'MAP.model.LookupData',
storeId: 'localCloudConditionStore',
proxy: {
type: 'localstorage',
id : 'cloud-conditions'
}
});
Here's code attempting to use removeAll()
to clear the local store:
offlineStore.load();
offlineStore.removeAll();
offlineStore.sync();
Ext.each(records,function(record){
var added = offlineStore.add(record.getData())[0];
console.log(added.get('id'));
});
offlineStore.sync();
What the local storage proxy writes to my browser:
And the Second reload does. it doesn't remove the prior ones:
If I try using proxy.clear()
, records go away, but the idGenerator seed isn't reset:
offline.getProxy().clear();
Ext.data.StoreManager.register(offline);
Ext.each(records,function(record){
var added = offline.add(record.getData())[0];
console.log(added.get('id'));
});
offline.sync();
Thanks for your eyeballs.
UPDATE
I've been able to reset the local storage's counter by directly reseting the value before instantiating my localstore. Obviously, this goes off the reservation, so it isn't ideal.
window.localStorage.setItem('cloud-conditions-counter',0);
var offline = Ext.create('MAP.store.LocalCloudConditionStore');
offline.getProxy().clear();
UPDATE DEUX
Upon further reflection, I think it's correct to not reset the counter. When a table in mysql is truncated, you don't reset the sequence number. That could cause problems if some cached id of a deleted record (let's say id of 1) was used to pull data from the table. A totally different record would be returned instead of the one that was deleted. Okay, makes sense.
Still, why the heck doesn't store.removeAll(); store.sync()
work?
Upvotes: 4
Views: 4158
Reputation: 23586
I'm not sure it is a bug, but I am sure that removeAll()
does not mark the records as deleted and will not affect remote or local storage (ie, a following sync won't do anything). This is in contrast to remove()
- which actually does so.
You can use this method to remove all records:
Ext.override(Ext.data.Store), {
removeAllForReal: function() {
records = this.data.items,
i = records.length;
while (i--) {
this.remove( records[i] );
}
}
});
Upvotes: 1