1. #1
    Sencha User
    Join Date
    Nov 2012
    Posts
    19
    Vote Rating
    0
    bothwell is on a distinguished road

      0  

    Default 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:
    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'}]
        }
    });
    Model:
    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' }
            ]
        }
    });
    Controller:
    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);
    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?

    Many thanks for any help :-)

  2. 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.

  3. #2
    Sencha User
    Join Date
    Nov 2011
    Location
    England
    Posts
    135
    Answers
    11
    Vote Rating
    7
    george.m is on a distinguished road

      0  

    Default


    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' }
            ]
        }
    });

  4. #3
    Sencha User
    Join Date
    Nov 2012
    Posts
    19
    Vote Rating
    0
    bothwell is on a distinguished road

      0  

    Default


    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.

  5. #4
    Sencha User
    Join Date
    Nov 2011
    Location
    England
    Posts
    135
    Answers
    11
    Vote Rating
    7
    george.m is on a distinguished road

      1  

    Default


    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.

  6. #5
    Sencha User
    Join Date
    Nov 2012
    Posts
    19
    Vote Rating
    0
    bothwell is on a distinguished road

      0  

    Default


    Oh, that's great - it does save data now, thanks for that. Appreciate the help!

  7. #6
    Sencha User
    Join Date
    Nov 2011
    Location
    England
    Posts
    135
    Answers
    11
    Vote Rating
    7
    george.m is on a distinguished road

      0  

    Default


    No problem.

Thread Participants: 1