segunda-feira, janeiro 26, 2009

jQuery UUID extension

This is a conversion of a few functions I make use of to a jQuery plugin.


This stems out from my need to be sure to have unique IDs, but I dont want to waste time reviewing code to make sure it is really unique, so as to not collide with other IDs on my page. I can have a random amount of forms at the same time in an inordinate amount of tabs, and each form has several form elements, and making sure they are all unique can be painstaking.


So I track my forms by registering the id of each form in an object array.


Naturally, other people may have other uses for the UUIDs.


UUIDs are generated in the format: 00000000-0000-0000-0000-000000000000 , with an optional prefix.


Usage 1: define the default prefix by using an object with the property prefix as a parameter which contains a string value;


jQuery.uuid({prefix: 'id'}); //returns nothing, sets default prefix to string 'id'

Usage 2: call the function jQuery.uuid() with a string parameter p to be used as a prefix to generate a random uuid;


jQuery.uuid('foo-'); // returns 'foo-<uuid>'

Usage 3: call the function jQuery.uuid() with no parameters to generate a uuid with the default prefix; The default prefix is '' (empty string)

jQuery.uuid(); //returns '<prefix><uuid>'

Get it at
http://plugins.jquery.com/project/uuid

Nenhum comentário: