简体   繁体   中英

Sencha Touch Global Variables

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

This is my 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 BaseURL and I can use it among all Data Stores and when need to change it, I just change this to reflect across whole application.

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.

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

You can declare a global variable normally as you would do without 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:

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

After some sencha updates we can do it:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM