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.