erichynds

Hi, I'm Eric Hynds, a front-end website developer living outside of Boston, Massachusetts. I'm passionate about developing functional, standard-compliant, and user-friendly websites.

Archive for April, 2010

Tips for Developing jQuery UI 1.8 Widgets

Thursday, April 22nd, 2010

I’m wrapping up my first jQuery UI widget (see multiselect on GitHub) and thought it would be useful to share some notes I took on the widget factory & widget development in general.  I personally found development on the factory to be quite enjoyable; a lot of functionality is available right out the box (custom events, ability to change options after initialization) and idioms you might not otherwise consider, like widget destruction.  Furthermore, it imposes a clean object-literal development structure with public and private methods, making it much easier to start a project or maintain other’s projects.

Throughout this blog post I will be showing you various ways to modify the jQuery UI dialog widget source to add your own features.  I do not actually recommend doing this however, because widgets can just as easily be extended.  The concepts are the same though and are easily adaptable to your own project. This post also assumes you are already familiar with the widget factory; if you aren’t, be sure to familiarize yourself first.

_init() and _create()

The widget factory automatically fires the _create() and _init() methods during initialization, in that order.  At first glance it appears that the effort is duplicated, but there is a sight difference between the two.  Because the widget factory protects against multiple instantiations on the same element,  _create() will be called a maximum of one time for each widget instance, whereas _init() will be called each time the widget is called without arguments:

$(function(){
 
	// _create() and _init() fire on the first call.
	$("div").mywidget();
 
	// a widget has already been instantiated on the div, so this time
	// only _init will fire.
	$("div").mywidget();
 
	// however, once the widget is destroyed...
	$("div").mywidget("destroy");
 
	// both _create() and _init() will fire.
	$("div").mywidget();
 
});

So how do you know where to place which logic?  Use _create to build & inject markup, bind events, etc. Place default functionality in _init().  The dialog widget, for example, provides an autoOpen parameter denoting whether or not the dialog should be open once the widget is initialized; a perfect spot for _init! (more…)

A New And Improved jQuery Idle Timeout Plugin

Monday, April 12th, 2010

This is a followup to an older post of mine about notifying & prompting your users for action once they’ve become idle. I’ve since re-written and abstracted the logic, creating a much more useful & customizable plugin. The main issue with my first implementation, which I’ve addressed in this update, is that the server was never actually polled to maintain an active server-side session during long periods of use. The server-side session might be set to expire after 30 minutes, but a user could be active on your application without a page load for longer than that. As such, following a page load after 30 minutes, the user would (presumably) be prompted to log back in… not a great user experience.

jQuery Idle Timeout

The gist of here is that after a user is idle after a configurable period of time, a div/UI dialog/whatever opens, warning the user that his/her session is about to expire. The user has X number of seconds to click a “resume” link in order to continue their session. If this event is not fired, the user is relocated (configurable) to a page where you’d likely implement server-side code to end their session. Idle state is one without any mouse movement or keypresses, and is detected with Paul Irish’s idleTimer plugin. (more…)