-
14 Jun 2011 12:55 AM #1
how to use global variable in Extjs 4
how to use global variable in Extjs 4
Hi !!!!!
How to use global variables in ExtJs 4. please give an example.
Thank you.
-
14 Jun 2011 1:40 AM #2
Globals! Generally a bad idea.
If I were you I'd wrap up the variables I needed to share in a singleton.
Can then access using MySharedData.foo, for instance.PHP Code:Ext.define('MySharedData', {
singleton: true,
foo: 'bar',
meh: 42
});
HTH,
WestyProduct Architect
Altus Ltd.
-
14 Jun 2011 1:57 AM #3
-
14 Jun 2011 5:09 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 435
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
14 Dec 2012 3:21 PM #5
Hi,
I would use this way to manage global variables but i have a problem with store definition.
My global variables definition :
I add Requires in my Ext.application :PHP Code:Ext.define('EM.Global', {
singleton: true,
urlGetData: 'server/getData.php',
imgLogoSite: 'obj/images/site/logo.gif'
});
I can use EM.Global.imgLogoSite in a View and it's ok.PHP Code:Ext.application({
requires: ['EM.Global',
'Ext.container.Viewport'],
name: 'EM',
appFolder: 'app',
autoCreateViewport: true,
(...)
But when i add in Ext.application store which use EM.Global.urlGetData for the url config, i have an error : EM is not defined (in Firefox):
And in Store :PHP Code:Ext.application({
requires: ['EM.Global',
'Ext.container.Viewport'],
name: 'EM',
appFolder: 'app',
autoCreateViewport: true,
stores: ['Users'],
(...)
I don't undestand where is my mistake.PHP Code:Ext.define('EM.store.Users', {
extend: 'Ext.data.Store',
requires: ['EM.Global',
'EM.model.User'],
model: 'EM.model.User',
proxy:{
type:'ajax',
url: EM.Global.urlGetData,
reader: {
(...)
Thank you for your help.
LaZag
(ExtJS 4.1.1a)
-
14 Dec 2012 5:54 PM #6
The line:
is being evaluated before Ext.define is called.Code:url: EM.Global.urlGetData,
Keep in mind that Ext.define is just a JavaScript function. The second argument passed to that function is an anonymous JavaScript object describing the class.
It is effectively equivalent to the following:
The requires block never gets a chance.Code:var config = { ... proxy:{ url: EM.Global.urlGetData, ... } }; Ext.define('EM.store.Users', config);
The simplest solution is just to make sure your globals get loaded first, maybe using a script tag. A more painful alternative is to move parts of the class definition into a callback function (third argument of Ext.define).
-
17 Dec 2012 4:27 AM #7
-
17 Dec 2012 4:33 AM #8
Product Architect
Altus Ltd.
-
18 Dec 2012 5:47 AM #9
-
18 Dec 2012 8:20 AM #10
Something like:
We use this trick where a class requires another class in its actual definition.Code:Ext.require([ 'EM.Global' ], function() { Ext.define('EM.store.Users', { extend: 'Ext.data.Store', requires: ['EM.Global', 'EM.model.User'], model: 'EM.model.User', proxy:{ type:'ajax', url: EM.Global.urlGetData, reader: { (...) });
It should be avoided if possible, but can be used when necessary.
Another way would be to define your proxy in the constructor, since requires will be loaded before a method is callable.Product Architect
Altus Ltd.


Reply With Quote
