May 26th, 2010 by radu

I like ExtJs and the way it is so modular. This time I needed to have something very easy, which is not implemented by default in Ext, so I rolled my own. I needed windows to fire ‘tofront’ and ‘toback’ events. In an app I am working on, the user can have quite many windows opened and I needed to know programatically when a window comes to front (and to back). The approach was to override the toFront method in Ext.Window. Below you can see the code, with some basic comments.

NOTE: I know there are the activate and deactivate events on every window, but when a window triggers the ‘deactivate’ event, Ext.WindowMgr.getActive() still returns it as the active window. This is why I needed the ‘tofront’ and ‘toback’ events.

(function(){

    // get the previous implementation of the toFront method
    var prevToFront = Ext.Window.prototype.toFront;

    Ext.override(Ext.Window, {

        toFront: function(){

            //get the window manager of this window, or Ext.WindowMgr if it doesn't have one
            var manager = (this.manager || Ext.WindowMgr);
            //get the window which is currently to front
            var activeWindow = manager.getActive();

            prevToFront.apply(this, arguments);

            //only fire tofront and toback events if the current window was not already to front
            if (this != activeWindow){
                this.fireEvent('tofront');
            }

            if (activeWindow){
                activeWindow.fireEvent('toback');
            }

            return this;
        }

    });
})();

I needed to run code both before and after the default implementation of the toFront method. If I didn’t need this, an easier approach would have been to use createSequence:

Ext.override(Ext.Window,{
       toFront: Ext.Window.prototype.toFront.createSequence(function(){
             // code here.
       })
})

Nice ExtJs! Go try it!

2 Responses to “Window ‘tofront’ and ‘toback’ events”
fielstem says:

I am a ExtJS newbie, so apologies for the naive question. I’m still just reading code and trying to understand the javascript and ExtJS techniques. My question is, when completing the toFront method why do you return this? The method triggers the two events, as intended, so what purpose does returning this serve? Any explanation or pointing me to a useful resource would be greatly appreciated. Thanks!

radu says:

Hi Fielstem,

Thanks for the question. As you can see, I am overriding the toFront method. If you look in the sources for Ext.Window.toFront, you see it’s returning ‘this’, so I just keep the contract so as not to break other parts of the library. It is a common technique to return `this`, as it is useful for chainability. For example, having a reference to an Ext.Element you can chain methods like: el.show().addClass(‘test-class’).applyStyles({ border:’1px solid red’})… and so on. Same with windows, you can do win.toFront().show(). Hope this is useful. Radu.

Leave a Reply