-
14 Nov 2012 4:49 AM #1
Answered: What's wrong with my local storage?
Answered: What's wrong with my local storage?
I'm trying to add a record to local storage but my code doesn't seem to be working. Have I misunderstood the process?
Store:
Model:Code:Ext.define("EnablerApp.store.LoginSessions", { extend: "Ext.data.Store", requires:"Ext.data.proxy.LocalStorage", config: { model: "EnablerApp.model.LoginSession", proxy: { type: 'localstorage', id: 'enabler-auth-store' }, sorters: [{ property: 'dateCreated', direction: 'DESC'}] } });
Controller:Code:Ext.define("EnablerApp.model.LoginSession", { extend: "Ext.data.Model", config: { idProperty: 'id', fields: [ { name: 'id', type: 'int' }, { name: 'EnablerInstance', type: 'string' }, { name: 'EnablerUsername', type: 'string' }, { name: 'EnablerAuthentication', type: 'string' } ] } });
At the end of this, the final console.log() in the controller contains the record object as expected, but when I check the localstorage directly, the record hasn't really saved - instead I have a single record with "enabler-auth-store" as the key, and a null value ('enabler-auth-store' is the proxy id in the store config). Am I missing a step? Or just doing something incorrectly?Code:var authStore = Ext.getStore("LoginSessions"); authStore.add({ id: 1, EnablerInstance: accountPacket.login.instance, EnablerUsername: accountPacket.login.username, EnablerAuthentication: authResponse.authentication }); authStore.sync(); var record = authStore.findRecord('id', 1); console.log('recorded the following data: '); console.log(record);
Many thanks for any help :-)
-
Best Answer Posted by george.m
Try it without setting your own ID (i.e. remove the line 'id: 1' in the .add(), and change the idProperty in your model to 'auto'. I've had the problem in the past and it's because I was setting my own IDs and Sencha was giving me warnings about unique identifiers.
-
14 Nov 2012 5:13 AM #2
Add these additions in bold to your model, see if that does the trick.
Code:Ext.define("EnablerApp.model.LoginSession", { extend: "Ext.data.Model", requires: ['Ext.data.identifier.Uuid'], config: { identifier: 'uuid', idProperty: 'id', fields: [ { name: 'id', type: 'int' }, { name: 'EnablerInstance', type: 'string' }, { name: 'EnablerUsername', type: 'string' }, { name: 'EnablerAuthentication', type: 'string' } ] } });
-
14 Nov 2012 5:35 AM #3
Thanks for the response, George. Unfortunately it doesn't seem to have changed anything.
I do notice that the 'enabler-auth-store' object gets added to local storage as soon as the application loads, before the rest of the code runs, so it's not being added in place of the record I want to add. I don't currently know what the local storage for a Sencha Touch app looks like, so I don't know if having the proxy ID in there is normal or not.
-
14 Nov 2012 6:04 AM #4
Try it without setting your own ID (i.e. remove the line 'id: 1' in the .add(), and change the idProperty in your model to 'auto'. I've had the problem in the past and it's because I was setting my own IDs and Sencha was giving me warnings about unique identifiers.
-
14 Nov 2012 6:30 AM #5
Oh, that's great - it does save data now, thanks for that. Appreciate the help!
-
14 Nov 2012 6:32 AM #6


Reply With Quote