最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sencha Touch 2 Global Variables - Access everywhere - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jan 20, 2013 at 18:54 User97693321 3,3467 gold badges47 silver badges69 bronze badges asked Mar 27, 2012 at 9:04 RicardoRicardo 12.1k8 gold badges29 silver badges40 bronze badges 2
  • 1 How are you accessing the function? This works for me. Remember you should access it like: APP.app.setLoadedUser('test');. And yes, it can be any value. :) – rdougan Commented Mar 27, 2012 at 22:24
  • That was it. Submit as an answer so can make it yours :) Thanks for the protip – Ricardo Commented Mar 28, 2012 at 9:20
Add a ment  | 

3 Answers 3

Reset to default 4

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. :)

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.

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();
发布评论

评论列表(0)

  1. 暂无评论