views:

43

answers:

3

When should I be creating a plugin and when not? Is it better to create a plugin and stuff everything (Data, Logic, View) within a plugin and use it like a black box or to separate Data, Logic and View ?

+2  A: 

When should I be creating a plugin and when not? Is it better to create a plugin and stuff everything (Data, Logic, View) within a plugin and use it like a black box or to separate Data, Logic and View ?

When you decide to distribute/share your code with others that works out of the box or something easier for others to use.

I would also suggest you to have a look at:

Why create a jQuery plugin?

Sarfraz
Upvoted for an excellent link.
HurnsMobile
Sharing code is nice, and sharing plugins are nice, but there's code to be shared that doesn't much fit as a jQuery plugin, and jQuery plugins that are a good idea but not necessarily somethins sharable. Still, I like the link too :-)
Pointy
@Pointy: Thanks and +1 to you for a detailed answer with example code.
Sarfraz
@sAc, Thanks for the link but do you have any link explaining "Why NOT create a jQuery plugin?"
Ashit Vora
A: 

Depends... will you be needing to do the same thing over and over with few variations? Then yes. I think this should flow naturally--if you find yourself constantly having to modify the plugin to accomodate new functions, making it bloated, then you're probably better off without it.

smdrager
+1  A: 

I think that it's as simple as this: any time you're considering writing a function like this:

function myUsefulThing($j) {
  $j.find('.button').trigger('click');
  // ...
}

and you plan to call it like this:

if (itIsTime) myUsefulThing($('#mainDiv .bigTable'));

then you clearly need a plugin. To put it another way, when the focus of a piece of code involves the DOM, and you want to provide some facilities that need direction as to what part of the DOM should be affected, then you should write a plugin.

If you've got something to do that doesn't have anything in particular to do with any part of the DOM, then you could add a "global" jQuery function (like $.map() or $.ajax()), but the case for that is weaker. It does cut down on global namespace polution, but that's traded for jQuery namespace pollution. ()

Pointy