Fast jQuery each when you need $(this)

Most of the time you use the jQuery $.fn.each function you end up doing $(this) inside the function that is getting looped.  You want access the jQuery magic in your loop.  It’s always seemed weird to me that $.fn.each didn’t pass your function a jQuery version of the element, since you already selected it in order to do the $.fn.each call.  Well James Padolsey started playing and created a quickEach plugin that passes in a jQueryized version of this.  Then Ben Alman started playing with it and ended up with a $.fn.each2 plugin. It is optimize just for the case where you need a jQueryized version of this in the loop, it is slower then the built in $.fn.each function when you don’t.

// jQuery each2 plugin usage:
$('a').each2(function( i, jq ){
this; // DOM element
i; // DOM element index
jq; // jQuery object
});

// jQuery core .each method usage:
$('a').each(function( i, elem ){
this; // DOM element ( this === elem )
i; // DOM element index
$(this); // jQuery object
});


jQuery UI 1.8 Widget Factory

jQuery UI 1.8 is set to have its final release any moment now and with it comes an updated Widget Factory.  If you haven’t used the widget factory before it’s a great piece of code that does the generic jQuery plugin type of things for you.  You can read more about the widget factory in this older post I did about it.

In jQuery UI 1.8 the Widget Factory gets a few very welcomed changes.  First default options are no longer defined outside of the widget, you now add an options property to the object you are passing $.widget and it will take care of merging your defined default options with the options the plugin gets initialized with.  This also means that in order to set global default options on the core jQuery UI widgets you need to now use $.ui.foo.prototype.options rather than $.ui.foo.defaults.  Also you no longer have to define getter methods like you did in 1.7.  In 1.8 any method that returns a value other then the plugin instance ( this ) or undefined it will assume it’s a getter and return that value.  This is sweet because now you can define a method that is a chainable setter and a getter.  The option method now returns the full options hash when called without any paramaters.

The _init method has been renamed to _create and a new _init method was created.  Yes go ahead and read that again I had to do a double take too.  Basically _create is now called only once when the plugin is first initialized and should contain all the logic to set up the widget.  The new _init method gets called every time the plugin gets called without passing a name of a method.  So when you do: $(‘.selector’).dialog({ height: 530 }); the first time both _create and _init get called. If you call that again later only _init will be called. This was created to fix a misunderstanding many people were having with the dialog plugin. Many people were calling $(‘.selector’).dialog(); and a dialog would open up, then later in the code they wold call the same thing and expect the dialog to popup. By having _init be called every time there is not a method provided it lets the dialog decide if it should open back up if it’s called again.

The _setData method got renamed to _setOption to fit better with what is actually happening.  Destroy now handles removing all widget-namespaced events for the plugin that are bound to this.element or this.widget().  Meaning name space your events and you don’t have to do the extra work of cleaning them up.

One very sweet thing is now you can create a new widget that extends an existing widget.  Just by doing something like: $.widget(‘myCustomDatePicker’, $.ui.datepicker, { //all my custom methods }); which makes it very easy to create a custom widget that can expand upon and use functionality that is already in another jQuery UI plugin.

There are a few other changes but these are the ones that perked my attention.  If you want more information check out the Upgrade Guide for 1.8 and check out Scott Gonzalez’s jQuery UI example code for migrating the widget factory from 1.7 to 1.8.  If you’re including the jQuery UI core in to your page and not using the jQuery UI Widget Factory to create your plugins you should start asking yourself WHY?



jQuery focusoutside event

I was doing my part helping out on StackOverflow in the jQuery tag yesterday and ran across a question asking how to hide a div on blur of an input field as long as the click wasn’t inside a div that “connected” to that input field.  What I ended up with for an answer was to bind a click and a focusin event on the document and check of the event.target was or is a child of the div we care about.  The focusin event is a special focus event the bubbles since the normal focus event does not. I needed to use the focusin event incase the user tabbed to the next field rather then clicking. This got me thinking, Ben Alman created a custom jQuery event ‘clickoutside’ for those times when you need to know that the user just clicked outside of your div and you should now close it. If the same code could also handle the focusin event then it would make answering that question very slick.  So I forked Ben Alman’s clickoutside event on github and did some hacking to add the focusoutside event.  I am sure the code could be shorted up even more then it is now but it works nicely.

UPDATE

My hacking inspired Ben to do some hacking of his own and we worked on making the plugin work for all the native jQuery events that made since to have outside versions.  Check out the new jQuery Outside Events.



jQuery is a’movin

Lot’s has happen in the past month with jQuery.

14 days of jQuery – We had 14 days of great links videos and info leading up and after the release of jQuery 1.4.

jQuery API – the jQuery API doc’s got a much needed overhall and are now running on WordPress.  Some very slick searching and the ability to comment on the documentation has brought live back in to the docs.

jQuery 1.4 was released in to the wild with tons of bug fixes, lots of speed enhancements and a few nice new features sprinkled on top to make it the prefect candy for all jQuery users to quickly flock to.

jQuery Forums packed up and moved from Google to Zoho which has produced a spam free forum again for the jQuery community

jQuery UI 1.8 rc1 has been released with a final release soon to come, bringing us 2 new widgets, button and autocomplete, along with a lot of bug fixes and improvements including an overhauled widget factory.

jQAIP an alternative viewer of the jQuery Doc’s was released for those who don’t like the design or workings of api.jquery.com.  Has its own nice search features and keyboard navigation.

jQuery Source Viewer – a quick and easy way to search through the jQuery source code.  Something that every jQuery developer should start doing after they learn the basics of jQuery.

I am looking forward to updating my projects to jQuery 1.4 and jQuery UI 1.8.  Will be interesting to see what the jQuery community can produce next.



Event listener add remove add remove . . . .

Something I have seen in many people have problems with on Stack Overflow, jquery’s google group, and forums is people who are assigning an event listener and then because something happens, normally because the thing is being animated, they kill the event listener and then once the animation is finished they add it back. You end up mucking with the element far more then you really need to.

Example of what I am seeing:

var doclick = function(){
    $(this).animate({'left':200},function(){
        $(this).bind('click',doclick);
    }).unbind('click');
};
$('li').bind('click',doclick);

You have duplicated code and will get very messy very quickly as more functionality gets added.

It would be much better to check if the thing is animated and if so just exit:

var doclick = function(){
    if ($(this).is(':animated')) return false;
    $(this).animate({'left':200});
};
$('li').bind('click',doclick);

No adding and removing the event listeners just decide if you really care if was clicked.
It doesn’t event need to be if its animated you can just use a flag to decide:

var flag = false;
var doclick = function(){
    if (flag) return false;
    $(this).animate({'left':200});
};
$('li').bind('click',doclick);
$('div.stopclicks').bind('click',function(){
    flag = true;
});


Think about your users first before getting out the paint brush

I am working on slicing up a pretty large site that a group of 3 designers have been working on. They are creating psd files for almost every page to define the layout and modules used on each page. After looking over the designs I started seeing things that look very “pretty” but make it so the user has to think or do something that is not what they naturally would do.

On the news section of the site the page has a list of the different news articles that have been published. When you click on an article to read it a “lightbox” type popup will open up with the article content. Having the article open up in a lightbox type of popup in the page is new and different but I don’t know if I would call it bad. It does let make it very quick and easy to get back to the list but it I don’t know if its that much better then loading new content in the page and then clicking the back button rather then a close button.

If that news article is longer then the defined height of the “lightbox” the designers wanted the article to be split up in to pages and to add the slick looking page buttons at the bottom. Wait, you want the user to click a button and have what they are currently reading removed and replaced with the next “page” of content, making it so they can’t look back at the rest of the article.  How many people are just going to decide that the rest of the article isn’t worth it and just close the “lightbox”?

The user is going to get to the bottom of the first “page” of the article and spin the mouse wheel a few times, when that doesn’t work he is going to look for a scroll bar. After he finds that there isn’t a scroll bar he is going to re-read that last sentence and make sure he really was right in thinking that there is more content. Then he is going to finally see those slick looking page buttons and click on the next arrow.

The design makes the user have to think multiple time just to read the content that we are trying so hard to get him to read.  Yes there are times when making a user learn something new makes the usability better, but it has to be worth the users time to spend learning.  When you are trying to get a user to read your content you want it to be as simple and easy for him to do that, not make him think about why he just spend 30 seconds trying to figure out how to get more content.