[jQuery] Re: Plugin Authoring Help pt 2

2008-03-05 Thread Danny

By the way, you'll have trouble if you use defaults like this:
$.fn.myPlugin1 = function(options) {
var myOptions = $.extend(defaults, options);

since that will actually extend the defaults object and your
defaults will be changed whatever was in options for the next time
you use the plugin.
Try
$.fn.myPlugin1 = function(options) {
var myOptions = $.extend( {}, defaults, options);
which creates a new object to accumulate the options.

Danny

On Mar 4, 2:13 pm, Mike Alsup [EMAIL PROTECTED] wrote:
 If you want to use two different functions in the chain then you need
 to define two plugin functions:

 // wrap it all in a closure so you can share private data easily
 (function() {

 $.fn.myPlugin1 = function(options) {
 var myOptions = $.extend(defaults, options);
 // do stuff

 };

 $.fn.myPlugin2 = function(options) {
 var myOptions = $.extend(defaults, options);
 // do stuff

 };

 var defaults = {
def1: 'default1',
def2: 'default2',
...
defx: 'defaultx'

 };
 })();
 On Tue, Mar 4, 2008 at 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/Authoring
   http://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.


[jQuery] Re: Plugin Authoring Help pt 2

2008-03-05 Thread jquertil

ugh, I' min the same boat. I hope some of the superstars will
eventually get around to writing more detailed tutorials. been
struggling with extension writing myself.

On Mar 4, 11:09 am, 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.


[jQuery] Re: Plugin Authoring Help pt 2

2008-03-05 Thread J Moore


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.


[jQuery] Re: Plugin Authoring Help pt 2

2008-03-05 Thread Chris Jordan
J,

I for one sure hope that someone follows up on your particular thoughts
here. I've not thought of doing what you're talking about here, and I'd love
to read other experts opinions on the subject.

Thanks for adding to this thread. :o)

Chris

On Wed, Mar 5, 2008 at 8:57 AM, J Moore [EMAIL PROTECTED] wrote:



 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.




-- 
http://cjordan.us


[jQuery] Re: Plugin Authoring Help pt 2

2008-03-04 Thread Mike Alsup

If you want to use two different functions in the chain then you need
to define two plugin functions:

// wrap it all in a closure so you can share private data easily
(function() {

$.fn.myPlugin1 = function(options) {
var myOptions = $.extend(defaults, options);
// do stuff
};

$.fn.myPlugin2 = function(options) {
var myOptions = $.extend(defaults, options);
// do stuff
};

var defaults = {
   def1: 'default1',
   def2: 'default2',
   ...
   defx: 'defaultx'
};

})();



On Tue, Mar 4, 2008 at 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/Authoring
  http://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.