Reputation: 120268
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.
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...
Upvotes: 0
Views: 2046
Reputation: 5398
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.
Upvotes: 2
Reputation: 111950
If the functionality involves modifying a set of nodes that vary between calls then definitely create a jQuery plugin.
Upvotes: 2
Reputation: 2803
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.
Upvotes: 1
Reputation: 359966
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.
Upvotes: 1