From here and here and some other googleing I can see that there is possible to have global constants, always defined within the code. What I need is to declare a XML or JSON file with these constants so that if I need to change these constants do not change source code. How can I achieve this using Extjs?
Asked
Active
Viewed 1,066 times
1 Answers
2
In your root project directory you can create a directory like 'data/' and add a JSON file there, like constants.json
.
Then when your app starts, you can make an ajax request to that file and store it somewhere, a singleton for instance.
Constants.js:
Ext.define('MyApp.Constants', {
alternateClassName: ['Constants'],
singleton: true,
SOME_CONSTANT: 'Abc', // you can also add values that are not part of the file
TIMEOUT: null // if you like to make it clear what values you are expecting, from constants.json
});
Somewhere, maybe in RootController
Ext.Ajax.request({
url: 'localhost:1841/data/constants.json',
method: 'GET'
}).then(function (result) {
var data = Ext.Json.decode(result.responseText);
Ext.Object.each(data, function (key, value) {
Constants[key] = value; // assuming all key-values in result are valid constants...
})
})
data/constants.json:
{
"TIMEOUT": 60000,
"FOO": "BAR"
}

leonard.javiniar
- 539
- 8
- 25