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.

Posts Tagged ‘jquery ui’

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 Quick Gotcha With CFML & JSON Serialization

Monday, March 29th, 2010

ColdFusion internally represents structure keys in uppercase when the keys are created using dot notation. Dot notation is typically how programmers write their code and is considered to be best practice when working with structures. To the same effect, ColdFusion’s implementation of serializeJSON() serializes your object with respect to case; therefore, we might end up with something like this:

<cfset struct = {} />
<cfset struct.foo = "bar" />
 
<cfdump var="#serializeJSON(struct)#" />
 
<!---
	Result:  {"FOO":"bar"}
--->

Bummer. You’d probably expect “FOO” to be lowercase since that’s how you wrote it, but unfortunately it’s uppercase according to CF. Serializing a query forces uppercase JSON keys as well because queries act and function similarly to structures. This is rather annoying seeing how JavaScript is case sensitive, and serializeJSON is the way to push data from CF to JS. jQuery UI’s Autocomplete is a fine example of a widget that was not designed with case in mind; your JSON is expected to be returned with either a label or value key or both… in lowercase. (more…)