Ricardo
Ricardo

Reputation: 12069

Sencha Touch 2 Global Variables - Access everywhere

I know this question was already posted in StackOverflow but I either didnt understand or sencha changed somewhat.

My app loads a form panel for login, then I would like to save the user info that have just loged on. This way I can change my view anytime I want and still know the name of the loged in user.

Here is my main code:

//<debug>
Ext.Loader.setPath({
    'Ext': 'sdk/src'
});

//</debug>
Ext.application({
    name: 'APP',

    loadedUser: 'the test',

    requires: ['Ext.MessageBox'],

    views: ['Main', 'Home', 'Login'],

    models: ['User'],

    stores: ['Users'],

    controllers: ['Main'],

    icon: {
        57: 'resources/icons/Icon.png',
        72: 'resources/icons/Icon~ipad.png',
        114: 'resources/icons/[email protected]',
        144: 'resources/icons/[email protected]'
    },

    phoneStartupScreen: 'resources/loading/Homescreen.jpg',
    tabletStartupScreen: 'resources/loading/Homescreen~ipad.jpg',

    setLoadedUser: function(arg) {
        this.loadedUser = arg;
    },

    launch: function() {
        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();

        // Initialize the main view
        Ext.Viewport.add(Ext.create('APP.view.Main'));
    },

    onUpdated: function() {
        Ext.Msg.confirm("Application Update", "This application has just successfully been updated to the latest version. Reload now?", function() {
            window.location.reload();
        });
    }
});

The 'loadedUser' its what I wanted to be my global variable, and the method setLoadedUser(arg) its suposed to change that value.

I can access 'loadedUser' no problem, but I can't change its value. Another question: loadedUser can it be an array/data structure?

Upvotes: 2

Views: 8448

Answers (3)

Mustafa Magdi
Mustafa Magdi

Reputation: 313

You can also use localStorage to, set/get Your variables:
Set it as: localStorage.setItem('currentUserId', userID)
Get it as: localStorage.getItem('currentUserId')
You can use it, anywhere in Your script.

Upvotes: 1

user552610
user552610

Reputation: 9

Yes, the works when the function is inside of the app.js file. It does not work if the function is inside of the controller file.

So if you have a project application called IronMan, the call from the view code to the global function, flyAway(), in your app.js file would look like:

IronMan.app.flyAway();

Upvotes: 0

rdougan
rdougan

Reputation: 7225

How are you accessing the function? This works for me. Remember you should access it like this:

APP.app.setLoadedUser('test');

And yes, it can be any value. :)

Upvotes: 4

Related Questions