2013-04-18 1 views
5

안녕 내 응용 프로그램의 어디서나 사용할 전역 변수를 정의해야합니다. 전역 변수 baseUrl을 내 app.js에 선언합니다. 이 글로벌 변수 sencha touch 2.1

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

      }, 
     } 



    } 

}); 

가 그럼 난 내 model.js에 basedUrl을 사용

//<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

을 app.js 아래를 참조하시기 바랍니다. 브라우저를 사용하여 볼 때 작동 할 수 있습니다. 내 응용 프로그램을 컴파일 엽차 응용 프로그램 빌드 테스트를 사용할 때,

나는 열 브라우저를 사용하고 나에게 오류 메시지가 catch되지 않은 형식 오류를 보여 주었다 : 정의되지 않은의 특성 'base을'을 읽을 수 없습니다. 어떤 생각?

+0

아래 답변이 도움이 되었습니까? – Gendaful

답변

16

프로덕션 빌드를 만들면 sencha 응용 프로그램의 모든 파일이 축소되어 전역 변수가 컨텍스트를 잃을 수 있습니다.

폴더의 유틸리티/Config.js

> 1 차 접근

이 폴더의 유틸리티에서 전역 변수를 선언/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(); 

두번째 접근 방식 ->

클래스 정의

var baseUrl; 

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

감사. work.horray !!!! – user998405

0

당신의 app.js에서

추가 간단한 예를하기 전에의 .js 파일을 전역 변수를 선언 변수들. 변수를 테스트하려면 Google 크롬 콘솔에서 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' 속성에 사용하고자하는 전역 변수에 대해서는 작동하지 않습니다 –