1
votes

i would like to consolidate the url base for my RESTFul API in a single place in my app built with Sencha Touch. Where is the best place i can put it?

There is a obvious option to store it in localStorage, but is this a good practice ?

3

3 Answers

2
votes

When I want to support MVC structure I create file Config.js and put it in application tree in the following place:

enter image description here

in Config.js:

Ext.define('MyApp.config.Config', {
    singleton: true,
    config: { /** here you can put any objects of your choice that will be accessible globally**/
        baseURL         : Ext.os.is.Android ? 'http://live_url_here.com' : 'http://localhost/testing_locally',
        topBannerUrl    : 'http://some_url/banner.png',
        anotherGlobalParam      : true
    },
    constructor: function(config) {

       this.initConfig(config);
       return this;    
    }
});

Those config parameters will be visible in the whole application.

You may get them:

MyApp.config.Config.getBaseImgURL(); /* returns 'http://some_url/banner.png' */
MyApp.config.Config.getAnotherGlobalParam(); /* returns true */

or set:

MyApp.config.Config.setBaseImgURL('new_url');
MyApp.config.Config.setAnotherGlobalParam(false);

This solution may be especially handy when your project requires many configuration parameters.
I hope it will work for you as well.

2
votes

Always keep your url base in a seperate file like util.js(utility.js). Your file path should be app > util > Util.js. You can keep your common functions like animateItem, showLoading/hideLoading, custom functions, etc over here so that you can use the same function throughout the app. To load this file in your app do this:

app.js

Ext.application({
    name: 'HelloWorld',        
    requires: [
     'HelloWorld.util.Util'
    ],
    view: []
})

For best practices in sencha touch you can see this: Sencha Touch Blog

1
votes

+1 for Anubis recommendation.

Something like this:

Ext.define('MyApp.Const', {
    statics:{
        url1:'....',
        url2:'....'
    }
})

Then you can access your urls with:

MyApp.Const.url1

Of course you must require Const class but you don't need to instantiate it.