Sencha Touch 2全局变量 - 随处访问

时间:2012-03-27 09:04:11

标签: javascript cordova global-variables sencha-touch-2

我知道这个问题已经发布在StackOverflow中,但我要么不理解,要么有些改变了。

我的应用程序加载了一个用于登录的表单面板,然后我想保存刚刚进入的用户信息。通过这种方式,我可以随时更改我的视图,并且仍然可以知道用户中的loged名称。

这是我的主要代码:

//<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/Icon@2x.png',
        144: 'resources/icons/Icon~ipad@2x.png'
    },

    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();
        });
    }
});

'loadedUser'是我想要的全局变量,方法setLoadedUser(arg)用来改变那个值。

我可以访问'loadedUser'没问题,但我无法更改其值。 另一个问题:loadedUser可以是数组/数据结构吗?

3 个答案:

答案 0 :(得分:4)

您是如何访问该功能的?这适合我。请记住,您应该像这样访问它:

APP.app.setLoadedUser('test');

是的,它可以是任何价值。 :)

答案 1 :(得分:1)

您还可以使用localStorage来设置/获取您的变量:
将其设置为: localStorage.setItem('currentUserId', userID)
得到它: localStorage.getItem('currentUserId')
您可以在脚本中的任何位置使用它。

答案 2 :(得分:0)

是的,当函数在app.js文件中时,它可以正常工作。 如果函数在控制器文件中,它不起作用。

因此,如果您有一个名为IronMan的项目应用程序,则flyAway()文件中从视图代码到全局函数app.js的调用将如下所示:

IronMan.app.flyAway();