PDA

View Full Version : Local Storage Model Update



fousheezy
17 Jul 2012, 9:28 AM
I've been wrestling with Sencha's local storage Store/Model/Identifier trifecta for a while. My goal is to be able to locally store a key/value pair where I can specify both values.

My store:


Ext.define('EM.store.Config', {
extend:'Ext.data.Store',
config: {
storeId: 'app-config',
autoLoad: true,
autoSync: true,
clearOnPageLoad: false,
model: 'EM.model.Config',
proxy: {
type: 'localstorage',
id: 'app-config-proxy'
}
}});


My model:


Ext.define('EM.model.Config', {
extend: 'Ext.data.Model',
config: {
identifier: 'uuid',
idProperty: 'id',
fields: [
{name: 'id'},
{name: 'configuration',type: 'string'}
]
}
});


But when I try to use it, I can save to the store, but not retrieve from it (when using Chrome)

I've tried a number of things, the most correct of which I believe is something along the lines of:

var s = Ext.getStore('app-config');
s.add({id:'my-id',configuration:'my-value'});
s.sync();
console.log(s.getById('my-id');


but if I refresh the page, I get a null back when doing
s.getById('my-id') instead of the expected value.

Can somebody please help me figure out why this is happening? Is this a known bug in Chrome? Or does Sencha not support saving models with ids that weren't auto-generated?

If we figure that out, I'd also like to know the ideal way to update a model entry and save it back to local storage.

I'm guessing I do something like this but there are literally no examples of the idiomatic way of doing this in Sencha.


var s = Ext.getStore('app-config');
var m = s.getById('my-id');
m.set('configuration', 'my-updated-value');
m.save();
s.sync();

mitchellsimoens
19 Jul 2012, 4:16 AM
There is currently a bug when trying to sync or save with localstorage proxy when you have a value for the idProperty fields (defaults to id)

fousheezy
19 Jul 2012, 5:39 AM
Ah, that's what I thought, I just wanted to make sure I wasn't inadvertently doing something wrong with the way I configured the store. Thanks!

So I guess I won't be able to use s.getById()

Would the next best way of working around this bug be to modify the model so it looks like


Ext.define('EM.model.Config', {
extend: 'Ext.data.Model',
config: {
// scheme for generating a unique id
identifier: 'uuid',
// id field is a throwaway field which gets an auto-generated value, key will store the actual value I want to use
fields: [
{name: 'id'},
{name: 'key',type:'string'},
{name: 'configuration',type: 'string'}
]
}
});

And then check for an extant key in the store by using findRecord


// for adding a unique key to the store, check if it is in the store already
var store = Ext.getStore('app-config');
var model = store.findRecord('key',myKey);
if (!model) {
// not in the store, add it
// don't include id field, as the record won't successfully save to the store
store.add({
key: myKey,
configuration: myConfig
});
} else {
// in the store, update the value of the model found
model.set('configuration', myConfig);
model.save();
}
// commit to local storage
store.sync();

mgdcaahmro
5 Mar 2014, 7:48 AM
is that the bug is still present ? Even in extJs 4.2.1 I have to remove idProperty from my model (because the data comes with a Id field and not a id field ...) No problem, because I use the Id field instead of model instance getId(), but i waste my time searching how to work around.