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 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:
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.
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.
()