I just uploaded a new copy of the code that handles getData/setData
seamlessly. Additionally I also add .option(pluginname, key, value)
which is equivalent to .data("key.pluginname", value).

Clocks in at about 51 lines of code.

--John



On Fri, Oct 3, 2008 at 4:49 PM, Yehuda Katz <[EMAIL PROTECTED]> wrote:
> A requirement not encapsulated in the set of requirements you mentioned
> above is:
> * It should be possible to add new options to existing plugins to make
> modifications based on the existing options/events in the plugin.
> The way I was suggesting doing it was similar to what I did in the tabs post
> I posted earlier today, and could have syntax something like:
> $.plugin.addOption("tabs", "ajax", function(val, instance) {
>     instance.bind("selected.tabs", function(e, target, container, startup) {
>         var href = $("a", target).attr("href");
>         if(!href.match(/^#/)) {
>           $($("a", target).attr("rel"))
>             .html("<div class='tabs-loading'>Loading</div>")
>             .load(href);
>         }
>       }
>     );
> });
> The idea here is that you could write a very simple plugin that mainly
> publishes some events, and that extensions (written by the plugin author or
> extenders) could do interesting things with the events or callbacks.
> -- Yehuda
> On Fri, Oct 3, 2008 at 10:03 AM, Jörn Zaefferer
> <[EMAIL PROTECTED]> wrote:
>>
>> I'm going to try this out by adpating various existing plugins to use
>> it. So far its a very good step in the right direction: Providing an
>> abstraction to write plugins that can be just as simple as adding to
>> $.fn, but with a clean API and the ability to use options, keep
>> internal state and automatic cleanup. Especially the last one has a
>> lot potential, as it makes every plugin much more robust by default,
>> while hiding the details about namespaced events.
>>
>> Jörn
>>
>> On Fri, Oct 3, 2008 at 6:41 PM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
>> >
>> > I like it. It's positive to keep working with names like setup and
>> > teardown (from events).
>> >
>> > --
>> > Ariel Flesler
>> > http://flesler.blogspot.com
>> >
>> > On Oct 3, 11:27 am, "John Resig" <[EMAIL PROTECTED]> wrote:
>> >> Hi Guys -
>> >>
>> >> At the Ajax Experience we talked about possibly making a reusable
>> >> function
>> >> for helping to encapsulate much of the functionality commonly seen in
>> >> jQuery
>> >> plugins.
>> >>
>> >> The important points seemed to be:
>> >>  - Plugins need a way to maintain state from one call to the next
>> >> (generally
>> >> in the form of 'options').
>> >>  - The state needs to be directly associated with the elements that
>> >> they're
>> >> called on
>> >>  - There needs to be a default set of options to load state from
>> >>  - Plugins need to clean-up any events or data that they bind to an
>> >> element
>> >>  - All methods introduced by a plugin should have access to the same
>> >> state
>> >>
>> >> I put my code up
>> >> here:http://dev.jquery.com/~john/plugins/widget/http://dev.jquery.com/~john/plugins/widget/widget.js
>> >>
>> >> This is how you would use it:
>> >>
>> >> The most basic call: Adds a single method (.test()) whose 'this'
>> >> represents
>> >> an individual element wrapped in a jQuery set. An empty options object
>> >> is
>> >> provided, as well.
>> >>
>> >> jQuery.plugin("test", function(a, b){
>> >>   this.options = a;
>> >>   this.hide(b);
>> >>
>> >> });
>> >>
>> >> jQuery("div").test("woo", "slow");
>> >>
>> >> Equivalent to the first style shown above.
>> >>
>> >> jQuery.plugin("test", {
>> >>   setup: function(a, b){
>> >>     this.options = a;
>> >>     this.hide(b);
>> >>   }
>> >>
>> >> });
>> >>
>> >> jQuery("div").test("woo", "slow");
>> >>
>> >> Next step up: A default set of options is provided - which implies that
>> >> the
>> >> first argument to .test() will be an options object.
>> >>
>> >> jQuery.plugin("test", {
>> >>   options: { speed: "slow" },
>> >>   setup: function(){
>> >>     this.hide( this.options.speed );
>> >>   }
>> >>
>> >> });
>> >>
>> >> jQuery("div").test({ speed: "fast" });
>> >>
>> >> Add in some related methods (related to the root setup method) that
>> >> also
>> >> have access to the instance and options.
>> >>
>> >> jQuery.plugin("test", {
>> >>   options: { speed: "slow" },
>> >>   setup: function(){
>> >>     this.hide( this.options.speed );
>> >>   },
>> >>   methods: {
>> >>     test2: function(){
>> >>       this.show( this.options.speed );
>> >>     }
>> >>   }
>> >>
>> >> });
>> >>
>> >> jQuery("div").test({ speed: "fast" }).test2();
>> >>
>> >> Remove some functionality added previously:
>> >>
>> >> jQuery.plugin("test", {
>> >>   options: { name: "test" },
>> >>   setup: function(){
>> >>     this.addClass( this.options.name );
>> >>   },
>> >>   teardown: function(){
>> >>     this.removeClass( this.options.name );
>> >>   }
>> >>
>> >> });
>> >>
>> >> jQuery("div").test({ name: "blah" });
>> >>
>> >> and the cleanup is triggered by (where 'test' is the name of the plugin
>> >> that
>> >> you wish to remove):
>> >>
>> >> jQuery("div").trigger("remove.test");
>> >>
>> >> It's not completely clear yet what will happen with the above plugin -
>> >> I
>> >> just wanted to put it out there since there was some interest in seeing
>> >> it.
>> >>
>> >> --John
>> > >
>> >
>>
>>
>
>
>
> --
> Yehuda Katz
> Developer | Engine Yard
> (ph) 718.877.1325
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to