2011-09-04 62 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(); 
    } 
});