2013-04-18 106 views
5

嗨我需要定义一个全局变量以在我的应用程序的任何地方使用。 我在我的app.js中声明了一个全局变量baseUrl。请参考下面 app.js全局变量sencha触摸2.1

//<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/[email protected]', 
     '144': 'resources/icons/[email protected]' 
    }, 

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

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

      }, 
     } 



    } 

}); 

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

我用浏览器打开,它给我的错误信息遗漏的类型错误:无法未定义读取属性“的baseUrl”。任何想法?

+0

下面的答案有帮助吗? – Gendaful 2013-04-18 23:20:45

回答

16

当您进行生产构建时,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]); 
    } 
}) 

的变化app.js

requires : [ 'App.util.Config'] 

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

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

第二Approach->

类定义

var baseUrl; 

Ext.define('classname,{Other things }); 
+0

谢谢。 work.horray !!!! – user998405 2013-04-19 07:28:17

0

一个简单的例子

在你app.js添加之前声明全局变量在你的.js文件变量。要测试您的变量,请在您的谷歌浏览器控制台中键入app.app.VariableName。 Chrome会为你自动完成。

Ext.application({ 
    name: 'app', 

    /** 
    * Custom Variables 
    * Use app.app.baseUrl to access the value 
    */ 
    baseUrl : 'http://example.com', 
    variable01 : 'foo', 
    variable02 : 'bar', 

    ... 

}); 
+0

此方法不适用于要在模型或商店中的“config”属性中使用的全局变量 – 2018-02-04 21:10:02