Global variable sencha touch 2.1

前端 未结 2 1158
说谎
说谎 2021-02-06 13:38

Hi I need to define a global variable to use in anywhere of my application. I declare a global variable baseUrl in my app.js. Please see below

相关标签:
2条回答
  • 2021-02-06 14:24

    A simple example

    In your app.js add the variables. To test you variables type app.app.VariableName within your google chrome console. Chrome will autocomplete for you.

    Ext.application({
        name: 'app',
    
        /** 
         * Custom Variables
         * Use app.app.baseUrl to access the value
         */
        baseUrl : 'http://example.com',
        variable01 : 'foo',
        variable02 : 'bar',
    
        ...
    
    });
    
    0 讨论(0)
  • 2021-02-06 14:31

    When you make the production build, all the files in your sencha app will be minified and thus the global variables may lose the context.

    There are several ways to declare global variables in your sencha app

    -> 1st Approach

    Declare a global variables in 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]);
        }
    })  
    

    Changes in app.js

    requires : [ 'App.util.Config']
    

    Now, you can use it in your application like as below.

    var baseUrl = App.util.Config.getBaseUrl();
    

    2nd Approach->

    Declare global variables in your .js files before the class definition

    var baseUrl;
    
    Ext.define('classname,{Other things });
    
    0 讨论(0)
提交回复
热议问题