ExtJS 5.0 – app.getController() fixes

Sencha
Right out of the box I’ve noticed that the getController() method on the Ext.app.Application object instances does not function correctly when accessing it from the global app property configuration. I am not sure if this is a bug Sencha has overlooked in the open source version of ExtJS 5 or not, but with a few simple changes you can suppress the errors and continue on your merry way.

Background Information

Before I begin I want to give a little background info about how these errors seem to come to light in my development. ExtJS has a application property called “appProperty”. When you set this property it enables you to access the app object globally from anywhere in your code like…

appName.app

The application object has a method called “getController” that you can use to get a reference to any controller in your application. It’s kind of the lazy man’s way of grabbing a reference in a complex app when you don’t want to or don’t have the time available to properly trace scope. It greatly helps for debugging scope issues and eliminating that as a factor. See ExtJS documentation for more information about the application object.

The problem is, right out of the box this functionality is broken for me. Luckily with just a few changes you can get it back in working order. To do this you will need to modify the code in /ext folder within your application (one level up from the /app folder). But in order to have it load this code you have to double check one thing in the “app.json” file first.

Inside your app.json file you need to look for the “js” block. If it looks like the below example then you need to modify it.
"js": [
{
"path": "${ext.dir}/build/ext-all-rtl-debug.js"
},
{
"path": "app.js",
"bundle": true
}
],

Change your block to match below:
"js": [
{
"path": "app.js",
"bundle": true
}
],

Now go find the following file in your /ext folder.
/ext/src/app/Application.js

You want to search for the method “getController”, this is where our changes are.

The first errors that I come across when trying to use getController() is
TypeError: controller.doInit is not a function

This will bring you to line 525 of Application.js
if (me._initialized) {

Change it to the following to suppress the error
if (me._initialized && typeof controller.doInit != "undefined") {

Now if you try to use getController() you’ll see a new error.
TypeError: c.getModuleClassName is not a function

To fix this one we need to look at line 507 of Application.js
className = c.getModuleClassName();

Modify this line to match
className = c.moduleClassName;

Now getController() should start behaving as it should! I’m not sure if this is just a bug in ExtJS they haven’t fixed or if I’m maybe using it wrong? but I somehow doubt that because i’m simply making a method call to the function with a valid controller path. Anyways. I hope this was of help to somebody else and possibly saved them some time and headaches.

Posted in Web Application Development Tagged with: , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*