views:

49

answers:

4

I have a bunch of methods that make use of jquery to basically generate a list view. The problem is I need to use this thing multiple times, so I need to make it reusable.

  1. Should I just write a jquery plugin?
  2. Should I write a non-jquery plugin Js Object (like a class in non-prototype languages) and just create instances as I need?

The primary concern is how to handle the case where I need to modify one of the methods that make up my plugin. So my reusable component has to be modifyable. I know how to do it with approach 2, not sure how to do it with approach 1.

edit -- to give an idea of what this is: its basically a list view plugin. So, it takes an xhr response, parses the xml/json inside, and adds a list of divs to a containing div. I was surprised I couldn't find an existing plugin to do this. Other js frameworks have this.

The reason it needs to be extensible is, it might have to deal with xml/json in the response -- so the handling of response is different. It might have to deal with outputting different templates for each response. etc...

+1  A: 

Just write a jQuery plugin. Your description is on the vague side, but I'd say that if you need to modify one of the methods in the plugin - and I'm not sure what degree of flexibility you need - you can just pass in a function that contains whatever specific logic, into your plugin's config object.

Matt Ball
awesome, two contradictory answers ;)
hvgotcodes
No, bears will eat YOU!
Will
(sorry, had an uncontrollable need to do that)
Will
@hvgotcodes - of course you're going to get two contradictory answers.
Matt Ball
+2  A: 

jQuery is basically a java script library, so writing you own library, or javascript file containing you user defined functions instead of a jQuery plug-in is not a sin. If there is no reason to make a plug-in, go ahead with option 2.

StudiousJoseph
awesome, two contradictory answers ;)
hvgotcodes
It's "jQuery", not "JQuery", and it's "JavaScript", not "java script" :D
J-P
Thank U guys!!! :P
StudiousJoseph
+1  A: 

I found it's always better to write a plugin that takes a hash as an argument. You can then use the old callback ||= false; if( callback ) callback(); trick for adding funcionality to your plugin initializer.

Don't forget to be careful with the this and scope stuff. If in confusion, this guide always come handy.

Erik Escobedo
That's basically what jQuery plugins do.
Matt Ball
Yeah. That's what I said: use a plugin :)
Erik Escobedo
+2  A: 

If the functionality involves modifying a set of nodes that vary between calls then definitely create a jQuery plugin.

J-P