« Return to Thread: Object-oriented plugins?
Re: Object-oriented plugins?
> 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.
You don't need a switch, you can call a method like this:
object['method_name']();
and also you can store the instance with $.data
This is what UI's widget factory was built for:
http://docs.jquery.com/UI/Developer_Guide
As for UI widgets you can call the method directly with $.data:
$("#tabbed").tabs();
$("#tabbed").data('tabs').add('test.html', 'new tab', 4);
But I'm not sure if this would be the encouraged way of using plugins,
actually I haven't heard about any convention you could follow when
writing an OO plugin. This questions comes up from time to time and it
seems it's up to you to pick a pattern.
On Nov 23, 4:03 am, "Hector Virgen" <djvir...@...> wrote:
> 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?
| Free embeddable forum powered by Nabble | Forum Help |