PDA

View Full Version : Load record from LocalStorage using Ext.ModelMgr



edrmp
13 Sep 2010, 12:47 PM
Hi,

I wondering if there are any way to load a record from LocalStorage with Ext.ModelMgr.

When I use Ext.ModelMgr.create, I successfully create and edit the record, using a form or using the console. However, when the object is already stored and is loaded from the store, the behavior is different, a new record, with the identifier "undefined" is created instead.

Also, If I create the record with Ext.ModelMgr, I can get the object with store.getById(1). But when the page is reloaded, no. Only store.getAt(0) appears to work in this case.

Some Code:




Ext.regModel('Person', {
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'}
]
});

store = new Ext.data.Store({
model: 'Person',
proxy: {
type: 'localstorage',
id : 'personst'
}//,
//autoLoad: true
});

store.read();

person = store.getAt(0);

if(!person){
person = Ext.ModelMgr.create({name:"John", age:30});
store.add(person);
store.sync();
}


person.set("name", "John B.")
store.sync()






So, I would like to know if I can load a record from the LocalStorage using Ext.ModelMgr. Or, if not, if I can bind the record obtained from the LocalStorage with the Model/ModelMgr anyway.

Any help would be appreciated.

Thanks

evant
13 Sep 2010, 6:13 PM
You need to give it some kind of id so it can refer to it again when the page loads:



Ext.regModel('Person', {
fields: [{
name: 'id',
type: 'int'
},{
name: 'name',
type: 'string'
}, {
name: 'age',
type: 'int'
}]
});

var store = new Ext.data.Store({
model: 'Person',
proxy: {
type: 'localstorage',
id: 'personst',
proxy: {
idProperty: 'id'
}
}
});

store.read();

person = store.getAt(0);

if (!person) {
console.log('creating');
person = Ext.ModelMgr.create({
id: 1,
name: "John",
age: 30
}, 'Person');
store.add(person);
store.sync();
}


person.set("name", "John B.")
store.sync();

edrmp
14 Sep 2010, 6:26 AM
Great evant!

Working fine now!

Thank you.

axxerion
15 Sep 2010, 10:46 AM
I am using 0.94... and that does not work for me.. my code is:

Ext.regModel('AxxerionList', {
idProperty: 'id',
fields: [
// id's
'id',
'objectNameId',
// basic info
'Field0',
'Field1',
'Field2',
'Field3',
'FieldIcon',
]
});

// here i create the store...

this.store = new Ext.data.Store({
model: 'AxxerionList',
proxy: {
type: 'localstorage',
id : 'temporalist1A'
}
});
this.store.read();

// i put some items inthere..
var itemsTemp = [];
....//create some items.
store.loadData(itemsTemp);
store.sync();

// I see in Chrome that a localstorage is created with the key="temporallist1A-<<id>>", where id is the idProperty... that is OK ...

// second time i do the following

this.store.read();
alert(this.store.getAt(0));

and the alert is given "undefined"....