PDA

View Full Version : Where do I define global constants for my app namespace?



abcdef
18 Aug 2011, 4:34 PM
Where do I define global constants for my app namespace?

Say -



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!

stevil
18 Aug 2011, 4:40 PM
If you want it at that level, you could do




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

or



MyApp.constants = {};

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


stevil

mitchellsimoens
19 Aug 2011, 6:16 AM
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.


MyApp.config = {....};

abcdef
23 Aug 2011, 5:24 PM
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.


MyApp.config = {....};

Ok. Simple enough. Thanks!

rex.staples
23 Sep 2011, 10:33 AM
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):


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:



Ext.application({
name: 'MyApp'
....
});

Ext.namespace('MyApp').config = {
...
};

ero
6 Dec 2011, 2:08 AM
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):


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:



Ext.application({
name: 'MyApp'
....
});

Ext.namespace('MyApp').config = {
...
};

+1 (Ext JS 4.0.7)

mitchellsimoens
6 Dec 2011, 6:17 AM
Ext.application({
name : 'MyApp',

launch : function() {
MyApp.config = {};
}
});

The problem you are having is you have to wait till the namespace is created.

thalsa
26 Feb 2014, 7:10 PM
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.


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