PDA

View Full Version : Problem with local storage when using sync()



Seb_ST
20 Dec 2012, 7:51 AM
Hi,

I have a problem similar to those discussed in the following threads : here (http://www.sencha.com/forum/showthread.php?198885)and here (http://www.sencha.com/forum/showthread.php?161595-How-to-update-existing-record-in-localstorage-store). However I wasn't able to solve my issue reading its.

I have a store for which I'm using a localstorage proxy and the sync() method seems to work properly when I am adding an object to the store, but not when I am modifying one.
I've tried to change the way I handle the idProperty in the model. I've tried to remove the idProperty and much more, but I am stuck there.

Can someone help me ?

Model :

Ext.define("CharacterSheet.model.Perso", {
extend: "Ext.data.Model",
requires: ['Ext.data.identifier.Uuid'],
config: {
identifier: 'uuid',
idProperty: 'id',
fields: [
{ name: 'id', type: 'auto' },
{ name: 'nom', type: 'string' },
{ name: 'campagne', type: 'string' },
{ name: 'langue', type: 'string', defaultValue: 'english'},
{ name: 'race', type: 'int', defaultValue: 1 },
{ name: 'PH_opt', type: 'boolean', defaultValue: 'false'},
{
name: 'typ_taille',
type: 'int',
convert: function(v, record) {
var races = Ext.getStore("Races");
var ra = record.get('race');
if (Ext.isEmpty(ra)) {ra = 1;}
return races.getById(ra).get('taille');
}
}],
}
});

Store :

Ext.define("CharacterSheet.store.Persos", {
extend: "Ext.data.Store",
requires: "Ext.data.proxy.LocalStorage",
config: {
model: "CharacterSheet.model.Perso",
proxy: {
type: 'localstorage',
id: 'perso-store'
},
sorters: [
{property: 'campagne', direction: 'ASC'},
{property: 'nom', direction: 'ASC'}
],
grouper: {
sortProperty: "campagne",
direction: "ASC",
groupFn: function (record) {
return record.get('campagne');
}
}
}
});

Code working :

var newPerso = Ext.create("CharacterSheet.model.Perso", {
campagne: 'test' ,
nom: 'Seb'
});

var persosStore = Ext.getStore("Persos");
persosStore.add(newPerso);
persosStore.sync();

Code not working : it actually updates my store but these updates don't survive a refresh

var persos = Ext.getStore("Persos");
var options = this.getOptions();
var newValues = options.getValues();
nb = persos.getCount();
for (ii = 0 ; ii < nb ; ii++) {
ppp = persos.getAt(ii);
ppp.set("langue", newValues.langue);
ppp.set("PH_opt", newValues.PH);
}
persos.sync();

mitchellsimoens
22 Dec 2012, 7:19 AM
Have you inspected what the localstorage looks like? Is it making it to the localstorage?

Seb_ST
22 Dec 2012, 7:55 AM
I can see that the fields have been modified in the localstorage. Even after refreshing the localstorage still show the records as I expect them to, meaning the changes are still appearing. But when I try to get the field I've modified with a code such as "mystore.getAt(0).get('field')" it returns the previous value. And it didn't before refreshing.

Seb_ST
18 Jan 2013, 8:32 AM
Anyboby ?