Sencha Touch全局变量

Sencha Touch Global Variables

本文关键字:全局变量 Touch Sencha      更新时间:2023-09-26

我正在开发Sencha Touch应用程序,我正在学习它,因为我喜欢JavaScript。

这是我的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. 如何在视图和存储中访问该变量

我建议将自定义全局变量添加到应用程序名称空间中,如下所示:

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

这样,你就可以在应用程序启动后访问这些自定义变量:

MyApp.app.apiToken

它也适用于函数:

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

你可以像不使用Sencha一样声明一个全局变量:

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

经过一些sencha更新后,我们可以这样做:

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

这个答案,可能对你有所帮助。
特别是当你想传递参数来存储