PDA

View Full Version : Access App variable?



grooveyorkcity
6 Jun 2011, 1:36 PM
I want to add a global variable to my app at creation time which contains the path to a service URL i use throughout several stores,etc in the app. What is the best practice for doing this? My app is defined as so:



Ext.regApplication({
name: "syz",
defaultUrl: '',
defaultTarget: "viewport",
icon: 'resources/images/icon.png',
glossOnIcon: false,
phoneStartupScreen: 'resources/images/phone_startup.png',
tabletStartupScreen: 'resources/images/tablet_startup.png',
/**
* This is called automatically when the page loads. Here we set up the main component on the page - the Viewport
*/
launch: function () {
this.viewport = new bgb.views.Viewport({
application: this,
listeners: {
click: this.onSelectionChange
}
});
},
});


I saw what looked like a similar setup in the o'reilly example, but it was structured differently and not using mvc/regapplication.

themightychris
6 Jun 2011, 3:12 PM
Ext.regApplication({
name: 'MyApp'
,apiUrl: 'http://foo.bar'
});



somewhere else after regApplication has been run:


console.info('apiUrl is: %o', MyApp.apiUrl);

Riaz
6 Jun 2011, 3:51 PM
How do I send value to 'apiUrl'?

themightychris
6 Jun 2011, 4:34 PM
Well you can declare it inside regApplication like in my previous example, or assign to it later:



MyApp.apiUrl = 'http://bar.foo';

realkevinroth
6 Jun 2011, 6:56 PM
Start by initializing it at the top of your program. It can be set before your first line of code.


MyApp.Url = "";

You'll have that as a global variable. It's initially set to a 0 length string. You can then assign it any time throughout your program. It's just a global variable. You can see what it is at any time by typing MyApp.Url into the console if you're using google chrome, or use


console.log(MyApp.Url); inside your program to see what it is as your program runs.

grooveyorkcity
7 Jun 2011, 7:06 AM
Thanks guys, this did the trick. Appreciate the help!