全局变量sencha touch 2.1

use*_*405 5 global-variables sencha-touch-2.1

嗨,我需要定义一个全局变量,以便在我的应用程序的任何地方使用.我声明一个全局变量的baseUrl在我app.js.请参阅下面的app.js

    //<debug>
Ext.Loader.setPath({
    'Ext': 'touch/src',//Location of the sencha touch source files
    'bluebutton': 'app',



});
//</debug>



Ext.application({
    name: 'bluebutton',//Application Path, all classes in you app. For eg blueButton.view.Main.case sensitive

    views: ['Main',

    'BlueButton.CouponMain',
    'BlueButton.CouponList',
    'BlueButton.CouponList2',
    'BlueButton.CouponList3',

    'BlueButton.TransactionMain',


    ],



   stores : [
   'BlueButton.GlobalVariable',


   ], 

    models : ['BlueButton.GlobalVariable',
    'BlueButton.MemberDetail',


     ],



    controllers: ['Main', 
    'BlueButton.MemberList', 


    ],



    requires: [
        'Ext.MessageBox',

    ],

    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'
    },

    isIconPrecomposed: true,

    startupImage: {
        '320x460': 'resources/startup/320x460.jpg',
        '640x920': 'resources/startup/640x920.png',
        '768x1004': 'resources/startup/768x1004.png',
        '748x1024': 'resources/startup/748x1024.png',
        '1536x2008': 'resources/startup/1536x2008.png',
        '1496x2048': 'resources/startup/1496x2048.png'
    },


    //--Global value--
    baseUrl: 'http://192.168.251.108:8080',
    //--Global value--



    launch: function() {

        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();


        // Initialize the main view

             var LoginLS = Ext.getStore('LoginLS');
             LoginLS.load();

             var record =  LoginLS.getAt(0);



            if(record != undefined){
                var sessionId = record.get('sessionId');
               if (sessionId !=undefined){
                        var mainView = Ext.getCmp("mainview");
                        if(!mainView){
                        mainView = Ext.create('bluebutton.view.Main');
                        }

                        Ext.Viewport.setActiveItem(mainView);  
               }
               else
               {
                        var loginView = Ext.getCmp("loginview");
                        if(!loginView){
                        loginView = Ext.create('bluebutton.view.Login');
                        }

                        Ext.Viewport.setActiveItem(loginView); 
                }
            }
            else{
                      var loginView = Ext.getCmp("loginview");
                        if(!loginView){
                        loginView = Ext.create('bluebutton.view.Login');
                        }

                        Ext.Viewport.setActiveItem(loginView); 



//                        //--Disable this line --
//                          var mainView = Ext.getCmp("mainview");
//                        if(!mainView){
//                        mainView = Ext.create('bluebutton.view.Main');
//                        }

//                        Ext.Viewport.setActiveItem(mainView);  
//                         //--Disable this line --





               }




//        Ext.create('bluebutton.view.TopMenuList');

    },

     init: function () {
        this.callParent(arguments);

    },


    onUpdated: function() {
        Ext.Msg.confirm(
            "Application Update",
            "This application has just successfully been updated to the latest version. Reload now?",
            function(buttonId) {
                if (buttonId === 'yes') {
                    window.location.reload();
                }
            }
        );
    }
});
Run Code Online (Sandbox Code Playgroud)

model.js

Ext.define('bluebutton.model.BlueButton.CouponList', {
    extend: 'Ext.data.Model',
    config: {
        idProperty: 'couponId',
        fields: [
            {  name: 'couponId'  },
            {  name: 'couponName'  },
            {  name: 'description'  },
            {  name: 'amount'  },
            {  name: 'couponType'  },

            {  name: 'merchant_bbID'  },
            {  name: 'sessionId'  },
            {  name: 'deviceId'  },



        ],

        proxy: {
            type: 'rest',
            url: bluebutton.app.baseUrl +'/WebCommon/rest/BBWebService/getCouponList',


            actionMethods: {
                create: 'POST',
                read: 'GET',
                update: 'PUT',
                destroy: 'DELETE'
            },


                      noCache: false, // get rid of the '_dc' url parameter

                    extraParams: {
                    sessionId: "1",
                      merchant_bbID: "merchant1",

                },


//            timeout:1000,
//            listeners: {
//                exception: function(proxy, response, operation) {
//                     alert("Connection Problem");
//                       Ext.Viewport.setMasked(false); // hide the load screen
//                       
//                  }
//               },

            reader: {
                type: 'json',
                 rootProperty: 'couponList'

            },

            writer: {
                type: 'json',

            },
        }



    }

});
Run Code Online (Sandbox Code Playgroud)

然后我在我的model.js中使用了basedUrl.它可以在我使用浏览器查看时工作.但是,当我使用sencha应用程序构建测试来编译我的应用程序时,

我用浏览器打开它,它向我显示一条错误消息Uncaught TypeError:无法读取未定义的属性'baseUrl'.任何的想法?

Gen*_*ful 16

在进行生产构建时,sencha应用程序中的所有文件都将缩小,因此全局变量可能会丢失上下文.

有几种方法可以在sencha应用程序中声明全局变量

- >第一种方法

在util/Config.js中声明一个全局变量

UTIL/Config.js

Ext.define('APP.util.Config', { 
    singleton : true,
    alias : 'widget.appConfigUtil',
        config : {
        baseUrl : 'xx.xx.xx.xxx',
    },
    constructor: function(config) {
        this.initConfig(config);
        this.callParent([config]);
    }
})  
Run Code Online (Sandbox Code Playgroud)

app.js的变化

requires : [ 'App.util.Config']
Run Code Online (Sandbox Code Playgroud)

现在,您可以在您的应用程序中使用它,如下所示.

var baseUrl = App.util.Config.getBaseUrl();
Run Code Online (Sandbox Code Playgroud)

第二种方法 - >

在类定义之前在.js文件中声明全局变量

var baseUrl;

Ext.define('classname,{Other things });
Run Code Online (Sandbox Code Playgroud)