Monday, March 30, 2009

[jQuery] How To Extend a jQuery: Plugins versus Other?

I've created a first jQuery plugin. I'm working in a project that uses
ColdFusion 5, and I'm being lazy about learning ColdFusion. I'm able
to spit out simple plain/text of this format from a database using
ColdFusion:

person.firstName=Alan
person.lastName=Gutierrez
person.address.city=New Orleans

And I convert them into hashes when they arrive from a $.get. I end up
with this JavaScript object expressed below in JSON.

var hash = {
person: {
firstName: "Alan",
lastName: "Gutierrez",
address: {
city: "New Orleans
}
}
};

I extend jQuery by extending the $.get method to detect a "hash" type
and wrap the callback in a function that will translate the property
file into a hash.

$.get( 'data.cfm?id=' + id, function( hash ) {
alert('Hello ' + hash.person.firstName + ' ' +
hash.person.lastName);
}, "hash" );

http://snipplr.com/view/13622/jquery-plugin-extend-get-to-parse-plain-text-properties-file-to-a-hash/

Best practice? Is there a better way to extend such core
functionality? I'm wondering how one goes about creating jQuery
extensions. It always bothered me that jQuery had such a small
namespace.

Alan Gutierrez

No comments: