Converting Ext to jQuery

July 24, 2008

I’ve recently been working on a fairly large javascript project that was using the Ext javascript library. Ext is a powerful javascript library that provides lots of UI widgets, in addition to a DOM manipulation API. The complete Ext library comes in at 546KB, and on this project we were only really using the Ext DOM functions, which made the library a bit overkill. The decision was made to use jQuery instead, which weighs in at only 16KB.

I set about replacing all the existing Ext code with jQuery equivalents. The majority of Ext functions map directly onto jQuery equivalents, which made things easier. For example:

Ext.get('id') -> $('#id')
Ext.get('id').query('.class') -> $('#id .class')

Some things don’t map quite so well, but are still relatively simple. The position functions are one such example:

Ext.get('id').getAbsoluteXY() -> [$('#id').offset().left, $('#id').offset().top]
Ext.get('id').bottom() -> $('#id').offset().top + $('#id').height()

However, some Ext functions have no equivalents at all in jQuery. Two Ext functions we were using with no jQuery equivalent were serialiseForm and decodeJson. I came up with the replacement functions below:

$.fn.decodeJson = function(string) {
		// if we don't have a string then there isn't any decoding to be done, return as is
		if(typeof(string) != "string") return string;
		// make sure the string starts with { and ends with }
		if (string.substr(0,1) == "{" && string.substr(string.length - 1,1) == "}") {
			eval("var decoded = " + string);
			return decoded;
		}
		return {};}

$.fn.serialiseForm = function(form) {
		// We might have been passed a form ID, or a form object
		if(typeof(form) == "string") form = jQuery('#' + form);

		// lets try ourselves!
		var getValuesFromForm = function(form) {
			var serialised = [];
			jQuery(form).children().each(function() {
				var child = jQuery(this);

				if (child.children().length > 0) {
					var t = getValuesFromForm(child);
					if(t.length > 0) jQuery.merge(serialised, t);
				}

				var name = child.attr('name');
				var type = child.attr('type');
				var val = child.val();

				if(name) {
					if (type != 'checkbox') {
						serialised.push(encodeURI(name) + '=' + encodeURI(val));
					} else {
						var checked = child.attr('checked');
						if (checked) {
							serialised.push(encodeURI(name) + '=' + encodeURI(val));
						}
					}
				}
			});

			return serialised;
		}

		var data = getValuesFromForm(form).join("&");
		return data;
}

The only feature of Ext being used without a relatively simple replacement was Ext.basicDialog, a UI widget. The jQuery BlockUI plugin came to the rescue:

dialog.hide() -> $.unblockUI();

During this rewrite I came to appreciate just how powerful jQuery selectors are. The codebase was considerably smaller after the rewrite, due to the expressiveness of jQuery. Many 3 or 4 liners in Ext would condense down to a single expression in jQuery!

5 Comments »

  1. You do know you don’t have to use the entire Ext package right? I think they even have a build customizer.
    That combined with gzipping should have made the file size a none issue. Could have saved you a lot of rework time :/

    I also think jQuery has JSON and form serialization:
    http://docs.jquery.com/Ajax/serialize
    http://docs.jquery.com/Plugins:Forms
    http://docs.jquery.com/Ajax/jQuery.getJSON

    - JDD

    Comment by JDD — July 25, 2008 @ 4:10 pm

  2. These are great examples. Thank you for sharing. If you haven’t already, you might take a look at jQuery UI http://ui.jquery.com/ . For example, the jQuery UI Dialog plugin provides another alternative replacement for Ext’s dialog. And since you mention blockUI, the equivalent would be dialog’s modal option:

    $(“#id”).dialog({ modal: true });

    Comment by Richard D. Worth — July 25, 2008 @ 4:20 pm

  3. I just think its silly to compare file size when sure the entire package of Ext may be 546KB, But jQuery + the entire uncompressed jQuery UI (excluding themes) is 677KB. I dig that you have shown devs how converting from Ext can be done with jQuery. I just think you did it for the wrong reasons.

    - JDD

    Comment by JDD — July 25, 2008 @ 4:24 pm

  4. @JDD – thanks for the links. I don’t know how I overlooked the serialize function! GetJSON isn’t a direct replacement for Ext’s decodeJSON though, which takes a string an returns an object. I don’t know of any jQuery function that does this.

    It was unfair of me to compare the jQuery filesize with that of the entire Ext library, but even with only the core Ext functions the library is still 145KB, almost ten times the size of jQuery. Size wasn’t the only concern though: We weren’t making use of Ext’s UI widgets, which is its the main strength. We were mainly just using DOM selection and modification functions which is where jQuery really excels, making it the best choice for this project.

    @Richard – thanks for you comments. For this project we don’t really require the more advanced UI widgets that jQuery UI provides. I’ll certainly be using it on other projects though!

    Comment by admin — July 26, 2008 @ 1:46 pm

  5. I don’t know which server-side system you’re on, but if you’re on ASP,NET you might want to check out Ra-Ajax (http://ra-ajax.org)

    Comment by Thomas Hansen — August 27, 2008 @ 12:14 am

RSS feed for comments on this post. TrackBack URL

Leave a comment