Turn your ExtJS 4 singletons into heavy and useful workers


A singleton is a class that can have only one object instance. In ExtJS 4 a singleton object is instantiated once the application is loaded. I love to use singletons as helpers in my applications. Recently I wrote an article about how to use glyphs with the FontAwesome (link).

In this article I will show you how I use a singleton call Glyphs to make references to icon names a bit easier. But there are some more useful tips on turning your singletons into heavy and useful workers.

In the FontAwesome article a glyph was addressed like this:

But ‘xf002@FontAwesome‘ is quite hard to remember if you are using many glyphed icons in your application. It would be so much nicer to just put here:

You can use the following class in your application to make this happen very easy. I have called the class Glyphs and I have added the methods: setIcon, getIcon and getGlyph that are all doing the same thing, getting the icon.

Some remarks about the usage

Notice that for the "del" (as in delete) I have not used the word "delete", for if you are using Sencha Command, you can’t use any identifiers that have reserved JavaScript names. Otherwise it will not pass the YUI compressor and ends up in syntax errors.

You can’t use this when you use Sencha Architect, for it will not accept a function on the "glyph" config of your button, it will convert it to string.

You can easily modify the class for usage with other webfonts.

Put this file in the "singleton" folder of your app and add it to the requirements of your app.js file.

Another singleton usage tip

Another handy way of using singletons is to create a toolbox of functions that are used more often in your applications. One example could be the rendering of certain fields that are used in different models. Instead of repeating the code over and over again, you can use a "Toolbox" or "Conversion" singleton.

In the class above you can see that the class has alternate names "Toolbox" and "Tools". To use references to items in this object, you have to use the object name "Toolbox" or "Tools" and not use this. When the renderer is calling the methode like ConvertOk, the reference this holds the panel or grid class or whatever you are calling from.

This is how you use the renderer (grid column):

or even shorter:

The methods enableFormFields and disableFormFields are handy if you want to enable or disable all fields in a form with one call. Use it like this:

or when your panel is already the class itself:

Johan van de Merwe
Dedicated to professional software development since 1985. Has worked since 1992 as IT manager in several international operating companies. Since 2007 CEO and Sencha Ext JS web application developer at Enovision GmbH.


  1. Instead of using setIcon in your button example, shouldn’t you use getIcon as set infers that you are going to set something not get?

    Also, for the delete vs del, you could wrap delete in quotes so it’s a string not a reserved word usage.

    • Johan van de Merwe

      Maybe you’re right, well it sets the icon on the display. It was more that I modified it about 30 times in an application, before I saw that I called it “getIcon”. So I just made it myself easy on this one.

      On the ‘delete’ you’re right. Strangely I am not very much used to use quotes in javaScript objects. Thanks for your feedback.

Leave a Reply