PDA

View Full Version : Sync Store with localStorage



bstras21
9 Dec 2011, 7:25 AM
Hello, is it possible to sync an entire store with localStorage? Or do I have to loop the records and add them individually?

Here is my store that loads fine:


var store = new Ext.data.Store({
model : 'Companies',
getGroupString : function(record) {
return record.get('id')[0];
},
proxy: {
type: 'ajax',
url : 'php/getCompanies.php',
reader: {
type: 'json',
root: 'results'
}
},
autoLoad: true
});


Here I am trying to sync my store with localStorage and it's not working:


var localstore = new Ext.data.Store({
model : 'Companies',
getGroupString : function(record) {
return record.get('id')[0];
},
proxy: {
type: 'localstorage',
id : 'testingLocal'
},
autoLoad: true

});
localstore.load();
localstore.add(store);
localstore.sync();

console.log('--------Main Store----------');
console.log(store);///I see all the records
console.log('--------LocalStore----------');
console.log(localstore);///I don't see any data


Do I have to loop the store and add the records individually?

AndreaCammarata
9 Dec 2011, 8:33 AM
Hi.
Take a look at this example I wrote you:



Ext.setup({
onReady: function() {

Ext.regModel('Contact', {
fields: ['firstName', 'lastName']
});


var store = new Ext.data.Store({
model: 'Contact',
sorters: 'lastName',
proxy: {
type: 'memory'
},
data: [
{firstName: 'Tommy', lastName: 'Maintz'},
{firstName: 'Rob', lastName: 'Dougan'},
{firstName: 'Ed', lastName: 'Spencer'},
{firstName: 'Jamie', lastName: 'Avins'},
{firstName: 'Aaron', lastName: 'Conran'},
{firstName: 'Dave', lastName: 'Kaneda'},
{firstName: 'Jacky', lastName: 'Nguyen'},
{firstName: 'Abraham', lastName: 'Elias'},
{firstName: 'Jay', lastName: 'Robinson'},
{firstName: 'Nigel', lastName: 'White'},
{firstName: 'Don', lastName: 'Griffin'},
{firstName: 'Nico', lastName: 'Ferrero'},
{firstName: 'Nicolas', lastName: 'Belmonte'},
{firstName: 'Jason', lastName: 'Johnston'}
]
});

var localstore = new Ext.data.Store({
model : 'Contact',
autoLoad: true,
proxy: {
type: 'localstorage',
id : 'testingLocal'
}
});

localstore.loadRecords(store.data.items);
localstore.sync();

console.log('-- LocalStore records count: ' + localstore.getCount());

}

});


Hope this helps.

bstras21
9 Dec 2011, 8:54 AM
I just test yours and it works but mine does not. I was able to get the same results by looping the store and using add(). Is this fine to do it this way? Also I checked the localStorage and see the following:


testingLocal-9
{"id":9,"number":"","company_name":"Jay","password":"Robinson","host_id":""}
testingLocal-7
{"id":7,"number":"","company_name":"Jamie","password":"Avins","host_id":""}
testingLocal-13
{"id":13,"number":"","company_name":"Rob","password":"Dougan","host_id":""}
testingLocal-counter
14
testingLocal-8
{"id":8,"number":"","company_name":"Jason","password":"Johnston","host_id":""}
testingLocal
1,2,3,4,5,6,7,8,9,10,11,12,13,14
testingLocal-12
{"id":12,"number":"","company_name":"Nigel","password":"White","host_id":""}
testingLocal-5
{"id":5,"number":"","company_name":"Ed","password":"Spencer","host_id":""}
testingLocal-1
{"id":1,"number":"","company_name":"Aaron","password":"Conran","host_id":""}
testingLocal-6
{"id":6,"number":"","company_name":"Jacky","password":"Nguyen","host_id":""}
testingLocal-11
{"id":11,"number":"","company_name":"Nicolas","password":"Belmonte","host_id":""}
testingLocal-3
{"id":3,"number":"","company_name":"Dave","password":"Kaneda","host_id":""}
testingLocal-14
{"id":14,"number":"","company_name":"Tommy","password":"Maintz","host_id":""}
testingLocal-10
{"id":10,"number":"","company_name":"Nico","password":"Ferrero","host_id":""}
testingLocal-4
{"id":4,"number":"","company_name":"Don","password":"Griffin","host_id":""}
testingLocal-2
{"id":2,"number":"","company_name":"Abraham","password":"Elias","host_id":""}


Does this look correct? I was assuming I would see one key and one serialized value? Thanks for all your help.

AndreaCammarata
9 Dec 2011, 9:01 AM
Yes it seems to correct and works anyway.
However, this could be a little bit slower while you are working with a large amount of data.
I guess my example doesn't works with your code just because you have an "ajax" proxy on your store.
For this reason when you try to get your store records to add them to the "localStore" you still didn't got the records from the sever side.
So maybe you should load the localstorage data on your main store "load" or "update event.