Search code examples
sencha-touch-2

Sencha localStorage.getItem() returns null in model


In my model, I have the following code:

Ext.define('Proximity.model.CandidateblocklistModel', {
    extend: 'Ext.data.Model',
    requires: ['Ext.data.proxy.LocalStorage'],
    config: {
        store:'Proximity.store.CandidateblockStore',
        fields: [
            { name: 'id', type: 'id' },
            { name: 'name', type: 'string' },
            { name: 'img', type: 'string' },
            { name: 'designation', type: 'string' },
            { name: 'summary', type: 'string' },
            { name: 'experience', type: 'string' },
            { name: 'industry', type: 'string' },
            { name: 'functionnml', type: 'string' },
            { name: 'role', type: 'string' }
        ],
        proxy : {
            type : 'ajax',
            url : Proximity.util.Config.getBaseUrl() + '/index.php/candidate/getcandidateblock',
            withCredentials: false,
             useDefaultXhrHeader: false,
            extraParams: {
                "id": localStorage.getItem('id')
            },
            reader : {
                    filters: [
                    Ext.create('Ext.util.Filter', {
                        property: 'name'
                    })
                ]
            }
        }
    }
});

The id in the local storage is already set before calling this model. I can see the id in localStorage by inspect element in Chrome, and I did get the value of it in other section. But I only can't get it in my model when I am trying to use it in proxy. I want to get data from my web service based on the value of the localStorage.

Code in my proxy:

extraParams: {
    "id": localStorage.getItem('id')
},

I want to get the id from localStorage here.

Please help me.


Solution

  • I think the following code works

    proxy : {
                type : 'ajax',
                url : Proximity.util.Config.getBaseUrl() + '/index.php/candidate/getcandidatebest',
                withCredentials: false,
                 useDefaultXhrHeader: false,
                extraParams: {
                    id: localStorage.getItem('id')
                },
                reader : {
                        filters: [
                        Ext.create('Ext.util.Filter', {
                            property: 'ind_id',
                            property: 'fun_id',
                            property: 'role_id',
                            property: 'id'
                        })
                    ]
                }
            }
    

    and then use the filtering facility of store to pass the localstorage value. To do that give filter permission remoteFilter: true, this.