View Full Version : Controller variables

8 Mar 2012, 2:47 PM
Hi, in my controller's init function I have:

init: function(){
var me = this;
this.myInternalVar = 1;

And I defined a function to get the value of the variable as this:

getInternalVar: function(){
return this.myInternalVar;

The problem is that getInternalVar() allways returns "undefined".

How can I define an internal variables in my controllers to be accessed by external objects?.

8 Mar 2012, 3:05 PM
I think this is a problem of scope. How do you access getInternalVar()?

8 Mar 2012, 3:18 PM
The controller is more or less like this:

Ext.define('MyApp.controller.User', {
extend: 'Ext.app.Controller',

init: function() {
this.myInternalVar = "value";

getInternalVar: function() {
return this.myInternalVar;

I want to get the value of the internal variable myInternalVar from an instance of the controller, like:


8 Mar 2012, 3:29 PM
Maybe the controller you refer to has not been initialized yet. You must initialize it first. This is done by declaring it in controllers config of your application or you must call its init() method. this.getController('MyApp.controller.User') only create an instance of your controller if it does not exist but it does not call its init() method.

8 Mar 2012, 3:35 PM
Not in this case. this.getController('MyApp.controller.User').getInternalVar(); is called from the launch method of the application, and init() is called, because I can set some "console.log(..)" and see the logs in chrome's console.

8 Mar 2012, 3:46 PM
What does your controllers config in your application look like?

8 Mar 2012, 3:50 PM
Here it is:

controllers: [

8 Mar 2012, 4:06 PM
So, your problem is that MyApp.controller.User is created and registered with id set to 'User' not 'MyApp.controller.User'. So when you refer to it by calling this.getController('MyApp.controller.User'), your application will create other instance of MyApp.controller.User with its id is set to 'MyApp.controller.User'. This instance is not the same as the instance that has been launched by your application. There are some ways to fix this problem:

Use shorthand of controller name in controllers config and in calling getController(). This way, you should fix your code to this.getController('User').
Use full name of controller class both in controllers config and in calling getController().

8 Mar 2012, 4:08 PM
Wow!, great answer!.

Thank you very much.