Results 1 to 8 of 8

Thread: Where do I define global constants for my app namespace?

  1. #1
    Sencha User
    Join Date
    Mar 2010
    Posts
    492
    Answers
    12
    Vote Rating
    2
      1  

    Default Answered: Where do I define global constants for my app namespace?

    Where do I define global constants for my app namespace?

    Say -

    Code:
    Ext.application({
        name: 'MyApp',
        ...
    });
    and I'd like to have constants for my app - e.g. MyApp.ENABLE_BLAH.

    Where do I specify these?

    Thanks!

  2. I often have a config.js file in the same root as my app.js file (that creates the Ext.app.Application). If you include the config.js after your app.js, the namespaces should be created. So in my config.js file, I can create an Object to hold config settings.

    Code:
    MyApp.config = {....};

  3. #2
    Ext JS Premium Member stevil's Avatar
    Join Date
    Nov 2007
    Location
    Denver, CO
    Posts
    1,045
    Answers
    15
    Vote Rating
    10
      0  

    Default

    If you want it at that level, you could do

    Code:
    Ext.apply(MyApp, {
        ENABLE_BLAH: true,
        ENABLE_BLORT: false,
        OTHER_CONSTANT:1
    });
    or

    Code:
    MyApp.constants = {};
    
    Ext.apply(MyApp.constants, {
        ENABLE_BLAH: true,
        ENABLE_BLORT: false,
        OTHER_CONSTANT:1
    });
    stevil

  4. #3
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,385
    Answers
    3997
    Vote Rating
    1540
      0  

    Default

    I often have a config.js file in the same root as my app.js file (that creates the Ext.app.Application). If you include the config.js after your app.js, the namespaces should be created. So in my config.js file, I can create an Object to hold config settings.

    Code:
    MyApp.config = {....};
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  5. #4
    Sencha User
    Join Date
    Mar 2010
    Posts
    492
    Answers
    12
    Vote Rating
    2
      0  

    Default

    Quote Originally Posted by mitchellsimoens View Post
    I often have a config.js file in the same root as my app.js file (that creates the Ext.app.Application). If you include the config.js after your app.js, the namespaces should be created. So in my config.js file, I can create an Object to hold config settings.

    Code:
    MyApp.config = {....};
    Ok. Simple enough. Thanks!

  6. #5
    Ext JS Premium Member rex.staples's Avatar
    Join Date
    Jul 2007
    Location
    Austin, TX
    Posts
    35
    Vote Rating
    3
      1  

    Default

    None of the previously proposed solutions in the thread work (Ext JS 4.0.5); they all throw the same error: 'MyApp is not defined'.

    You can see in the code below the application is not immediately created (note the onReady):

    Code:
    Ext.application = function(config) {
        Ext.require('Ext.app.Application');
    
        Ext.onReady(function() {
            Ext.create('Ext.app.Application', config);
        });
    };
    I'd love to see the above code modified to formally support a best-practice for constants or at the very minimum define the namespace immediately, but until then, you can wrap your declaration with Ext.namespace:


    Code:
    Ext.application({
        name: 'MyApp'
        ....
    });
    
    Ext.namespace('MyApp').config = {
        ...
    };

  7. #6
    Sencha User
    Join Date
    Oct 2010
    Location
    France
    Posts
    37
    Answers
    2
    Vote Rating
    0
      0  

    Default

    Quote Originally Posted by rex.staples View Post
    None of the previously proposed solutions in the thread work (Ext JS 4.0.5); they all throw the same error: 'MyApp is not defined'.

    You can see in the code below the application is not immediately created (note the onReady):

    Code:
    Ext.application = function(config) {
        Ext.require('Ext.app.Application');
    
        Ext.onReady(function() {
            Ext.create('Ext.app.Application', config);
        });
    };
    I'd love to see the above code modified to formally support a best-practice for constants or at the very minimum define the namespace immediately, but until then, you can wrap your declaration with Ext.namespace:


    Code:
    Ext.application({
        name: 'MyApp'
        ....
    });
    
    Ext.namespace('MyApp').config = {
        ...
    };
    +1 (Ext JS 4.0.7)

  8. #7
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,385
    Answers
    3997
    Vote Rating
    1540
      0  

    Default

    Code:
    Ext.application({
        name : 'MyApp',
    
        launch : function() {
            MyApp.config = {};
        }
    });
    The problem you are having is you have to wait till the namespace is created.
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  9. #8
    Sencha User
    Join Date
    Mar 2013
    Posts
    2
    Vote Rating
    0
      0  

    Default MyApp not ready at time of config.js include

    Quote Originally Posted by mitchellsimoens View Post
    I often have a config.js file in the same root as my app.js file (that creates the Ext.app.Application). If you include the config.js after your app.js, the namespaces should be created. So in my config.js file, I can create an Object to hold config settings.

    Code:
    MyApp.config = {....};

    I included the config.js after app.js in the index.html using a javascript tag, MyApp is not defined which is normal since probably the document is not ready, unless you mean to include it some otherways. one other thing is that the constants in config.js are to be used in different places in the app but I need to wait for the Myapp to be ready in order to define the constants, isn't a chicken and egg problem?, I probably missed something, could you please elaborate more on how to include it?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •