Search code examples
sencha-touchsencha-touch-2

Get values from localStorage to use in Sencha Touch AJAX Proxy


In my Sencha Touch application I store some values in localStorage and they get stored just fine. Later I need to use those values for a request to a store and I need to pass those values as request headers. The code looks like the following:

Ext.define('AppName.store.storeName', {
    extend: 'Ext.data.Store',

    requires: [
        'AppName.model.modelName'
    ],

    config: {
        model: 'AppName.model.modelName',
        storeId: 'storeName',
        proxy: {
            type: 'ajax',
            url: 'http://dynamic',
            headers: {
                auth_token: localStorage.getItem('activeUserToken'),
                user_email: localStorage.getItem('userEMail')
            },
            reader: {
                type: 'json'
            }
        }
    }
});

However, both auth_token and user_email values are being returned as "undefined" when in Chrome developer tools I clearly see those values. Besides of that I access those values in many other places in my Sencha Touch application but can't do that in the store itself.

Could anybody please tell me what I'm doing wrong?

Thanks


Solution

  • I've solved this problem in the following way. Instead of passing the headers in the store I'm passing the headers during the request, before loading the store. Here's the code for that:

        Ext.getStore('storeName').getProxy().setUrl(someURL);
            
        Ext.getStore('storeName').getProxy().setHeaders(
             {
                 "auth_token" : activeUserToken,
                 "user_email" : userEmail
             }
        );
    
        Ext.getStore('storeName').load();