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.



SSH host name completion on a Mac

The list of servers I have to ssh in to is continually growing many of them I need to have an ssh tunnel to get to so I fully use the ~/.ssh/config file to help me out. Now that my .ssh/config file has grown I am starting to forget what name of that one server was. On some unix boxes bash_completion is installed which allows you to type: ssh se[tab] and get: ssh servernamethatisreallylog.com. bash_completion is not installed on the Mac by default. You can install it with macports but I have yet to run in to a real need to install macports yet so I started searching for any other ways to get this working on my Mac.

What I found was a comment on a MacOSXHints hint.
All you need to do is throw this in your ~/.bash_profile file.

_complete_ssh_hosts ()
{
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
                        cut -f 1 -d ' ' | \
                        sed -e s/,.*//g | \
                        grep -v ^# | \
                        uniq | \
                        grep -v "\[" ;
                if [ -f ~/.ssh/config ]; then
                        cat ~/.ssh/config | \
                                grep "^Host " | \
                                awk '{print $2}'
                fi
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh

This will read in your .ssh/known_hosts file and your .ssh/config file and create a list of host names that you can now tab complete with the ssh command. Comes in very handy.



Stateful jQuery plugins with jQuery UI’s widget factory

There are many different formats for jQuery plugins, many of the formats are not designed for stateful plugins.  What is a stateful plugin? A plugin that keep track of what the state of the plugin is and lets you call methods and change properties even after the plugin as been initialized.  All of jQuery UI’s plugins are stateful (as of version 1.7.2), part of the jQuery UI core is a “Widget Factory” which makes it very easy to quickly make a stateful plugin.

Below is an example of a plugin created with the jQuery UI widget factory.

$.widget('namespace.pluginName', {
	_init: function() {
		// stuff that is called on initialization of the plugin

		//this.options a combination of the defualt options and the ones passed in during the plugin initialization
		if (this.options.foo) {
			//this.element is the element that the plugin was called on
			this.element.fadeOut();
		}
	},
	_privatemethod: function() {
		//private internal function
		//private functions should be named with a leading underscore
		//will only be able to be called from inside the plugin
	},
	publicmethod: function() {
		//this is a public fuction that can be called outside of the plugin

		//calling the private method from inside the public method
		this._privatemethod();
	},
	value: function() {
		//this is a public function that is defined as a getter
		//meaning it returns a value not a jquery object
		return this.options.foo;
	},
	destory: function() {
		$.widget.prototype.apply(this, arguments); // default destroy
		// this is where you would want to undo anything you do on init to reset the page to before the plugin was initialized.
	}

}));

$.extend($.namespace.pluginName, {
	getter: 'value',
	defaults: {
		option1: 'bar',
		foo: true
	}
});

With this you can call the following to initialize the plugin

$('#myelement').pluginName();

To call the publicmethod function you can now do this after initializing the plugin:

$('#myelement').pluginName();
$('#myelement').pluginName('publicmethod');

This makes it very easy to create a stateful jQuery plugins very quickly. All you need is the core of jQuery UI, but most people already have the whole jQuery UI package on their page.



Work around for Google Gears on Snow Leopard

As I talked about in by previous post, Google Gears is not compatible with Snow Leopard.  Google has been slow to get out a fix for the problem but seems like some others have found a work around and the guys from MailPlane have published how to use it.  It involves downloading the source code, applying a patch and then building a package, or you can just download the package from MailPlane, though they have a “Use at your own risk” disclaimer on it. You will also have to run Safari in 32bit mode. So if you are dieing to get your Google Gears back, especially in light of the gmail outage on September 1st, then try it out.  Let me know if you run in to any problems.

http://mailplaneapp.com/download/google_gears/