This post is over 6 months old. Some details, especially technical, may have changed.

jQuery 1.5 Released

Official Post: http://blog.jquery.com/2011/01/31/jquery-15-released/

Quick post just spread the word that jQuery 1.5 dropped today which is right on time according the the schedule :) .  I guess it's a bit pointless because the people that care already know this, but humour me!

New Stuff

There isn't a heap of "new to jQuery" stuff in this release which I guess is a good thing.  The MS contributed plugins have stayed just that - plugins.  Previous talk of templating becoming absorbed into the jQuery core in 1.5 has failed to bear fruit.  Good stuff - not sure why it should have been a core function - there is no one right templating strategy/engine and putting one in the core is sure to annoy as many people as it will please others.  So what's new?

Deferred Objects

Deferred Objects are objects that return a contract that will be adhered to regardless of when actions are bound to it.  This forms the heart of the Ajax rewrite which I'll touch on in a minute.  This is essentially the Promise implementation pattern.  Basically when you execute a function that returns a Deferred object you can keep attaching callbacks to that object and they will get executed even if they are bound after the function completes whatever operation it has to do.  The jQuery example demonstrates this very well,

// Assign handlers immediately after making the request,
// and remember the jxhr object for this request
var jxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jxhr.complete(function(){ alert("second complete"); });

So basically at any point in time we can attach callbacks to the ajax promise, pass it around our system, let other functions attach handlers etc and it will all get resolved/executed when the call has completed.  It's nice stuff though it may not have a TON of uses.

jQuery.sub()

Similar in a way to the YUI3 sandboxing technique jQuery.sub() lets us create a clone or subclass of the global jQuery object and hack it to bits for our own needs without affecting the global jQuery object.  Again limited uses but when you need it it can be a godsend.  Here is a very contrived example,

(function($){
    var sub$ = $.sub();

    sub$.ajax = function(){
        // some sort of stub for ajax testing
    };

    // run tests involving ajax here
    // ... 
})(jQuery);

You see what I am doing?  Say I wanted to perform some sort of unit test involving calls to the Ajax method.  I could, in isolation, make the Ajax method return whatever I want without worrying about breaking other tests. 

Improved/Refactored Stuff

Ajax Rewrite

The ajax module has been rewritten entirely.  It will now return a special jQuery based XHR object (rather than a plain XHR object).  This object is a deferred object that I spoke about above.  This means we can pass the ajax request around and let components or modules attach their own handlers when they need to.  We can also bind multiple callbacks via the jQuery chaining pattern (see example above)

Moar Speed

As usual many of the DOM traversal methods have been given performance tweaks - .children(), .prev(), and .next().  The official post has shiny graphs and such so hop on over there for a full run down.

Conclusion

I hate that word but I don't have another word I like better.... Yes so I haven't played with this a great deal recently, just some superficial tinkering but it's a solid release that adds some nice features (with limited uses for most I suspect).  I mean they could have kept deferred objects internal - they were implemented for the Ajax stuff - but why not make it public - could be useful to many people, and that is a good attitude. 

Also good decisions were made to keep templating out of the core I think (others may disagree but that's life!).  Finally this is going to be the jQuery version used in jQuery Mobile when it hits shortly!

Published in JavaScript on February 01, 2011