extjsbuildenvironment-variables

ExtJS (5) Sencha build configurations for different environments


I have an ExtJS 5.0 app that points to an API. On development, I'd like to use http://localhost but on production of course some other server.

Is there a way to set an app's variable from the outside during sencha app build ? Or access some ENV variable in the code?

Would be great if anybody could help me.

Thank you very much,

Sebastian


Solution

  • I found a way of doing this but I am not sure it is the best way. In your app.json you will find a section like this:

    /**
     * override objects for setting build environment specific 
     * settings.
     */
    "production": {
        banana : 'no'
    },
    "testing": {
        banana : 'yes'
    },
    "development": {
        
    },
    

    Obviously I have added the banana field myself. We are not allowed to deploy a banana in production so I set the value yes or no based on the environment.

    Then at build whether I do 'sencha app build testing' or 'sencha app build production' it will build in the respective banana value for the environment into the Ext.manifest object.

    So in the application I can access the value through:

    if (Ext.manifest.banana === "yes")
    

    I presume you can use the same technique to define fields for other fruit, or perhaps even non-fruit related data.

    I got the idea from this document: "When you launch your application, you will find the processed content of app.json loaded as “Ext.manifest”." https://docs.sencha.com/cmd/6.5.0/guides/microloader.html

    Like I said I don't know if this is the best way to do it but I suspect it might be.