javascript Sencha Touch 全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7300097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:39:44  来源:igfitidea点击:

Sencha Touch Global Variables

javascriptsencha-touch

提问by Umair A.

I am working on a Sencha Touch application and I am learning it great because I love JavaScript.

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

This is my app.js

这是我的 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/";
    }
});

This is one of my Store.

这是我的商店之一。

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

Now the question is, if I can create a global variable across whole application? It's named BaseURLand I can use it among all Data Stores and when need to change it, I just change this to reflect across whole application.

现在的问题是,我是否可以在整个应用程序中创建一个全局变量?它被命名为BaseURL,我可以在所有数据存储中使用它,当需要更改它时,我只需更改它以反映整个应用程序。

I need to know two things.

我需要知道两件事。

  1. How to declare a global application level variable.
  2. How to access that variable in views and stores.
  1. 如何声明全局应用程序级变量。
  2. 如何在视图和存储中访问该变量。

回答by Martin Tajur

I would recommend adding your custom global variables to the application namespace, like this:

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

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

This way you will be able to access these custom variables after your application has launched:

通过这种方式,您将能够在应用程序启动后访问这些自定义变量:

MyApp.app.apiToken

It works with functions, too:

它也适用于函数:

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

回答by Luis

You can declare a global variable normally as you would do without Sencha:

您可以像没有 Sencha 一样正常声明全局变量:

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

And then to use it:

然后使用它:

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

EDIT :

编辑 :

If you want to declare it as a member of your App instance do:

如果要将其声明为 App 实例的成员,请执行以下操作:

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/";
    }
});

and then:

接着:

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

回答by Victor Azevedo

After some sencha updates we can do it:

经过一些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();
    }
});

回答by Mustafa Magdi

Thisanswer, may help You.
Specially if, You want to pass params to store.

这个答案,可能对你有帮助。
特别是如果,您想将参数传递给存储。