2011-09-04 60 views
3

我正在研究Sencha Touch應用程序,我正在學習它,因爲我喜歡JavaScript。Sencha Touch全局變量

這是我app.js

var App = new Ext.Application({ 
    name: 'My First App', 

    //BaseURL: 'http://mydomain.com/testing/first/services/', 

    launch: function() { 
     this.views.viewport = new this.views.Viewport(); 

     // this.BaseURL = "http://mydomain.com/testing/first/services/"; 
    } 
}); 

這是我的商店之一。

var newsStore = new Ext.data.Store({ 
    model: 'News', 
    sorters: [{ 
     property: 'PostedOn', 
     direction: 'DESC' 
    }], 
    proxy: { 
     type: 'ajax', 
     url: 'http://mydomain.com/testing/first/services/News.php', 
     reader: { 
      type: 'xml', 
      root: 'News', 
      record: 'New' 
     } 
    }, 
    getGroupString: function(record) { 
     if (record && record.data.PostedOn) { 
      return record.get('PostedOn').toDateString(); 
     } 
     else { 
      return ''; 
     } 
    }, 
    autoLoad: true 
}); 

現在的問題是,如果我可以在整個應用程序中創建一個全局變量?它的名稱爲BaseURL我可以在所有數據存儲中使用它,當需要更改時,我只需更改它以反映整個應用程序。

我需要知道兩件事。

  1. 如何聲明全局應用程序級別的變量。
  2. 如何在視圖和商店中訪問該變量。

回答

20

我會建議將您的自定義的全局變量的應用程序的命名空間,就像這樣:

Ext.application({ 
    name: 'MyApp', 
    launch: function() { }, 
    apiToken: 'foo' 
}); 

這樣,您就能夠訪問這些自定義變量在您的應用程序啓動後:

MyApp.app.apiToken 

它的工作原理帶功能:

Ext.application({ 
    name: 'MyApp', 
    launch: function() { }, 
    apiToken: 'foo', 
    getApiToken: function() { return this.apiToken; } 
}); 
2

可以正常聲明一個全局變量,你會不煎茶做:

var BaseURL = "http://mydomain.com/testing/first/services/"; 

然後使用它:

proxy: { 
     type: 'ajax', 
     url: BaseUrl + 'News.php', 
     reader: { 
      type: 'xml', 
      root: 'News', 
      record: 'New' 
     } 
    } 

編輯:

如果您想要將其聲明爲您的應用程序實例的成員:

var App = new Ext.Application({ 
    name: 'My First App', 
    launch: function() { 
     this.views.viewport = new this.views.Viewport(); 
     this.BaseURL = "http://mydomain.com/testing/first/services/"; 
    } 
}); 

然後:

proxy: { 
     type: 'ajax', 
     url: App.BaseUrl + 'News.php', 
     reader: { 
      type: 'xml', 
      root: 'News', 
      record: 'New' 
     } 
    } 
+0

應用實例裏面?我想在App實例內部進行操作並通過App實例訪問它。 – Neutralizer

+0

檢查我編輯的答案 – Luis

+0

另請參閱Ext.namespace()和Ext.Application文檔。 「name」屬性應該不包含空格,並且可以用作全局標識符。 – romaninsh

0

This答案,可以幫到你。
特別是,如果你想通過params存儲。

1

一些煎茶更新後,我們可以做到這一點:

var App = Ext.application({ 
    name: 'Myapp', 
    serviceUrl: 'https://example', 

    launch: function() { 


    } 
}); 


Ext.Ajax.request({ 
    url: Myapp.app.servideUrl, 
    withCredentials: true, 
    useDefaultXhrHeader: true, 
    method: 'post', 
    scope: this, 

    params: { 
     cmd:  'connect', 
     id:   login, 
     password: pass, 
     version: 1 
    }, 

    success: function (response) { 

     var result = Ext.JSON.decode(response.responseText);    

     if (result.result.status === 'connected'){ 
      this.loginSucess(result); 
     }else{           
      this.loginFail(result); 
     } 
    }, 

    failure: function (response) {      
     this.loginFail(); 
    } 
});