my current plugin is getting really big (just over 8000 lines right now) and i would like to know if there is a way to sort it into different files.
Just liike the require function in node.js, but for jquery, so it would be sorted into more files and thus, be more clearly arranged.
As @jcubic mentioned you need to have your code separated into individual modules / functionality purposes.
Store all of your methods in a method object of some sort (this could also be within the plugins namespace of course). This can also easily be added to, or even extended from a different file.
var methods = (function ($) {
return {
init : function () { initMethod(); },
another : function () { anotherMethod(); },
thirdThing : function () { thirdThing(); },
etcEtc : function () { etcEtc(); }
};
}(jQuery));
I highly recommend the method calling way of creating jQuery plugins, which would utilize this object.
$.fn.pluginName = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.tooltip');
}
};
Now everything is separated, and you call your modules by doing $('whatever').pluginName('methodhere'); etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With