How to extend and override without creating an extension or override.

September 17th, 2009 Jay Garcia

Many times Ext JS developers are required to extend or add functionality to a widget in the framework.  Some of the solutions include creating an actual extension using Ext.extend or even creating a plugin.  If the requirement is a only for a single-use and is relatively simple to implement, creating an extension or plugin class is overkill.   So the question is, what is the best solution for this type of situation?

I say it’s override and extend on-the-fly at the configuration object, which is possible for just about every class in the framework.   We’ll go into exactly how this works in just a bit.  Before that, I’d like for you to ponder a simple requirement, and we’ll explore an extremely simple one-off solution.

Requirement: Display an Ext JS Window that when closed, a confirmation dialog is requested.  If the actor presses “yes”, then the instance of Window actually closes with an alert confirming the window closing.  OK, I know that it sounds a little silly.  Trust me, in the nearly four years I’ve been developing applications with this framework, silly requirements like this have been presented to me.

OK, how do we solve this easily?  Here is an example that extends the close method, and introduces a new method (alertClose) to the instance of Ext.Window to be created.

Here’s how this works.  In the above code, we extend the close method.  The reason I choose the word “extend” is because, just like when creating an actual extension, we have the ability to call the Window.prototype.close method, which actually will complete the original execution flow.  If we chose not to call the prototype.close method, then this technically would be an override.

The close method will display the Ext.MessageBox.confirm dialog.  The dialog’s callback is an anonymous method that is scoped to the instance of Window that is being created.  It inspects the Button id, which defaults to ‘yes’ or ‘no’.  If the actor clicks ‘yes’, then the  newly added this.alertClose method is called,  alerting the actor that the window closed.  This is  followed by an Ext.Window.prototype.close is call, actually closing (and destroying) the instance of Window that is created.

If the actor clicks the “no” button, then the confirmation dialog hides and all is well again.  Below is the UI flow of the code above.

I’m sure you’re probably thinking: “Wait a minute?!  How the heck is that possible?”  It’s possible because in most classes, the following line exists in the constructor:
Ext.apply(this, config);

This line basically applies whatever is in the configuration object to the instance of the class (this), thus setting override parameters and methods.  This, of course, includes any new methods, such as alertClose (above).

Here it is in Ext.Component (Ext.Window is a descendant of Ext.Component):

And there you have it, you now know how to extend and override a single instance of a class without creating an extension or override.

Extensions and plugins are not obsolete however.  They still retain their value and hold their place in this world.  After I write the “Extensions and Plugins” chapter of Ext JS in Action, I’ll follow up with a somewhat comprehensive guide to constructing these.

and introduce the confirmation prompt.

Posted in EXT Examples, EXTJS, Uncategorized | 4 Comments »

How to clear your Jing cache quickly and easily.

September 4th, 2009 Jay Garcia

Posted in Uncategorized | No Comments »

Interact with me in the development of Ext JS In Action

March 16th, 2009 Jay Garcia

Please Join and follow me with Twitter at : http://twitter.com/tdginnovations so you may follow and interact with me during the Development of Ext JS In Action :)

Posted in Uncategorized | No Comments »

Douglass Crockford discusses AJAX Performance.

March 15th, 2009 Jay Garcia

A must watch!

Posted in Uncategorized | No Comments »

Ext Window Drawers Alpha 1

September 12th, 2008 Jay Garcia

 Ext Window Drawers Alpha 1 is released today with two new examples; the drag and drop and basic grid window in the desktop.

Click here to download: winDrawers.alpha1.tgz

The following features were added:

  • IE6 and IE7 Certification
  • Fx3 Certification
  • Titles
  • Support for [ X ] close button in titles
  • Resize finished
  • Window.el.move intercepted
  • animate : true || false honored

Examples to date:

(drag and drop example)

(grid in desktop window example)

Known issues:

  • IE6/7 – Issues with z-ordering (desktop only thus far)
  • Desktop window maximize to normal size not  hooked properly

Posted in Current Projects, EXTJS, Ext Extensions, Uncategorized | 4 Comments »