jQuery: The Write Less, Do More JavaScript Library

 « Return to Thread: Object-oriented plugins?

Object-oriented plugins?

by Hector Virgen :: Rate this Message:

Reply to Author | View in Thread

Does anyone know of a good example of an object-oriented plugin for jQuery?

The reason I ask is that the jQuery convention is to return this at the end of the plugin to allow for chaining. While that's great for simple plugins, some of my advanced plugins instantiate object that I would like to access at various points in my script.

From the looks of it, jQuery's "tabs" plugin seems to use an object stored "somewhere" (perhaps in the element). But to invoke methods on that object, you have to call the plugin again and pass it a string as its first parameter:

http://docs.jquery.com/UI/Tabs/tabs#.22enable.22index
jQuery#tabs( "add", url, label, [index] )

To be honest, this seems a little awkward for me. This means I would have to write my plugin to check the first parameter to see if it's a string or an object, and if it's a string I then would have to make a switch to call the approriate method based on the string value.

I would much rather be able to access the tabs instance directly:

var tabs = $('#tabbed').tabs(); // tabs is instance of jQueryTabs
tabs.add(url, label); // accessing instance directly

But if tabs acted like that, it would break chaining.

$('#tabbed').tabs().hide(); // This would error

Is there another way to create object-oriented plugins, while still following the jQuery conventions?

Thanks!

-Hector

 « Return to Thread: Object-oriented plugins?