You might find it easier to simply create objects that use jquery,
instead of writing a jquery plugin.

The biggest advantage is that you actually have a normal instance of
an object. You can pass this instance to other objects, call other
methods on it... all the usual good stuff. (jquery plugins seem to be
a one-shot deal. you call the method, pass in a bunch of parameters,
and it works. If you need to access that instance again, you can't. i
had this problem with the pagination plugin. i added more elements to
my list, but there was no way to tell the pagination object that the
list was longer. i would have to delete it and recreate it.)

here's the pattern i use. let's say i wanted a 'fancy' textarea box.

  function TextBox(opts) {
    this.jC = opts.container; // all jquery objects start with a 'j'
    this.visible = 0;
  };

  TextBox.prototype.draw = function() {
    this.jC.html('<textarea></textara>'); // could add lots of
functionality to the textbox. key press handlers, etc.
    this.visible = 1;
  };

  // how you use the object...
  $(document).ready(function() {
    var txt = new TextBox({container:$('#text')}); // obviously need
an element to be the container.
    txt.draw();
  });


Works well for me. Maybe one of the plug-in experts can comment on
when it makes sense to write a jquery plugin versus write a normal
object that uses jquery?

-j

On Mar 4, 2:09 pm, Leanan <[EMAIL PROTECTED]> wrote:
> Ok, I'm really trying to wrap my head around this, and it's irritating
> me.  I've checked out the following pages:
>
> http://docs.jquery.com/Plugins/Authoringhttp://www.learningjquery.com/2007/10/a-plugin-development-pattern
>
> And they help some but there's one problem I'm running into.
>
> I have:
>
> $.fn.myPlugin = function(options) {
>   $.myPlugin.defaults = $.extend($.myPlugin.defaults, options);
>   return(this);
>
> };
>
> $.myPlugin = {
>   defaults: {
>     def1: 'default1',
>     def2: 'default2',
>     ...
>     defx: 'defaultx'
>   },
>
>   myFunc: function() {
>     var options = $.myPlugin.defaults;
>     //do something here, anything really
>   },
>
>   myFunc2: function() {
>       var options = $.myPlugin.defaults;
>       //do something here, again, anything
>   }
>
> };
>
> Now, I want to do something like this:
>
> jQuery().myPlugin({def1: 'other_def1', def2: 'other_def2'}).myFunc();
>
> Can I?
>
> Because it's not working.  It tells me that jQuery().myPlugin({def1:
> 'other_def1', def2: 'other_def2'}) is not a function.
>
> However, if I change it so that myFunc is $.fn.myFunc = function()
> { }, then I can, but I have to do it as:
>
> jQuery().myPlugin({def1: 'other_def1', def2:
> 'other_def2'}).myPlugin.myFunc();
>
> Which I don't like.
>
> None of the tutorials or documentation I can find is clear on this
> point, though they do refer to the functions in $.myPlugin as being
> user-accessable... and they are, but only if I do $.myPlugin.myFunc.
> I can't chain.  And I want to be able to do that.

Reply via email to