Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to expand jquery.plugin over multiple files

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.

like image 415
F. Rick Reich Avatar asked Dec 13 '25 16:12

F. Rick Reich


1 Answers

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

like image 145
Mark Pieszak - Trilon.io Avatar answered Dec 15 '25 07:12

Mark Pieszak - Trilon.io