Creating a jQuery Plugin, How do I do Custom Scopes?

Posted by viatropos on Stack Overflow See other posts from Stack Overflow or by viatropos
Published on 2010-06-15T20:51:39Z Indexed on 2010/06/15 21:32 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

I would like to create a jQuery plugin with an API something like this:

$("#chart").pluginName().attr("my_attr");

Instead of these:

$("#chart").pluginName_attr("my_attr");
$.pluginName.attr("#chart", "my_attr");

Basically, instead of having to namespace every method that acts similar to ones in jQuery, I'd like to "scope" the methods to a custom api, where $("#chart).pluginName() would return an object such that get, attr, find, and a few others would be completely rewritten.

I'm sure this is not a well-liked idea as it breaks convention (does it?), but it's easier and more readable, and probably more optimized, than the two options above. What are your thoughts?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about api