I've written a plugin for writing plugins if you want to check it out: http://code.google.com/p/jquery-plugin-dev/source/browse/trunk/jquery.plugin.js
It basically allows you to use any object that you have created and applies it to the jQuery namespace, thus namespacing your object and allowing you to use 'this' to refer to the object set you have selected. You can apply it either to jQuery.fn or jQuery using: // static jQuery.plugin.add('pluginName', myObj); or: // instance jQuery.fn.plugin.add('pluginName', myObj); to now use your object statically: jQuery.pluginName.staticProperty; // or: jQuery.pluginName().instanceProperty(); jQuery.pluginName.staticMethod(); // or: jQuery.pluginName().instanceMethod(); to now use your object with a jQuery instance: jQuery('.my-selector').pluginName.staticProperty; // or: jQuery('.my-selector').pluginName().instanceProperty(); jQuery('.my-selector').pluginName.staticMethod(); // or: jQuery('.my-selector').pluginName().instanceMethod(); For your scenario, you could create an init() method and call it once: var obj = jQuery('.my-selector').pluginName().init(options); // then go: obj.login(); obj.logout(); I don't have an example page up yet, but you can send me any questions that you may have. I'll have a working site at: http://jquery.illax.in/ hopefully very soon. Cheers, Trey On Apr 16, 7:06 am, Steven Black <ste...@stevenblack.com> wrote: > Interesting, Byrne. > > The first thing I always look-to is "A Plugin Development Pattern" by > Mike Alsup > http://www.learningjquery.com/2007/10/a-plugin-development-pattern > > Therein Mike has a few good ideas about handling options. I really > like how Mike keeps the options accessible, as opposed to settable > only upon call. That's one factor to consider here. > > For a variety of reasons, I would recommend against designing this as > one monolithic plugin. The jqGrid plugin is a good example of how a > loader can be used to include or exclude facets. > > Speaking of facets, I wouldn't get too deep into this without first > examining the jQuery Aspect Oriented Plugin > --http://code.google.com/p/jquery-aop/ > -- because, by the sounds of it, this might be right-up your alley as > an approach. > > **--** Steve > > On Apr 15, 12:37 am, Byrne Reese <byrnere...@gmail.com> wrote: > > > First, some background: > > > I am one of the lead contributors to the Movable Type Open Source > > project and am currently engaged in writing a jquery plugin that would > > be used on all of the blogs the platform publishes. So far I have > > successfully written a surprisingly complex plugin that works like > > this: > > > $('#greeting').greet( options ); > > > Which would convert: > > > <div id="greeting"></div> > > > Into a message that would look like this: > > > Welcome Byrne! (edit profile | logout) > > > You get the idea. > > > The options I need to pass into the plugin is pretty substantial so I > > won't bother you with the specifics. > > > Here is my question: > > > I want my plugin to surface multiple event handlers like the > > following: > > > a) $('#id').greet( { loggedOutMessage: 'Welcome back %user! > > %logout' }); > > > This is from the example above. > > > b) $('#id').loggedIn( function(user) { $(this).show(); alert(u.name + > > ' logged in'); } ); > > > This would be invoked when a user successfully logged in and would be > > invoked whenever a login event occurred (which happens mainly via > > Ajax). One use case for this function is to selective show an element > > that would otherwise be hidden if the user was logged out. > > > c) $('#id').loggedOut( function() { $(this).hide(); } ); > > > Similar to (b) above, but this of course would respond to a logout > > event. > > > Ok, finally the question: I would like for all of the above use cases > > to be served by the same plugin. There is a lot of shared code between > > all three and a lot of shared config options between them. > > > I would like to somehow initialize the system once with the same > > config options, but then subsequently invoke the loggedIn, loggedOut > > and greet functions on various elements. > > > I guess I am just a little lost on the best way to do this and was > > wondering if there was any kind of best practice I should follow > > here. > > > Not knowing a lot about how I *should* do it, here are some ideas I > > have for what the client code could look like: > > > Idea 1: > > > $.mtInit( global_options ); > > $(document).ready( function(){ > > $('#greet').greet( greet_specific_options );}); > > > $('.hide_when_logged_out').loggedIn( function() { $(this).show(); } ); > > $('.hide_when_logged_out').loggedOut( function() { $(this).hide > > (); } ); > > > Idea 2: > > > $.mtInit({ > > cookiePath: 'foo', > > cookieDomain: 'bar', > > cookieName: 'baz', > > onLoggedIn: function() { $('#greeting').greet(); }, > > onLoggedOut: function() { $('.hide_when_logged_out').hide(); } > > > }); > > > Does anyone have any thoughts to share on the best or better way to do > > this? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---