3

私の現在のプラグインは非常に大きくなっており (現在は 8000 行を超えています)、それを別のファイルに分類する方法があるかどうか知りたいです。

node.js の require 関数に似ていますが、jquery の場合は、より多くのファイルに分類され、より明確に配置されます。

4

1 に答える 1

1

@jcubicが述べたように、コードを個々のモジュール/機能目的に分離する必要があります。

すべてのメソッドを何らかのメソッド オブジェクトに保存します (もちろん、これはプラグインの名前空間内にある場合もあります)。これは、別のファイルに簡単に追加したり、別のファイルから拡張したりすることもできます。

var methods = (function ($) {
    return {
        init       : function () { initMethod(); },
        another    : function () { anotherMethod(); },
        thirdThing : function () { thirdThing(); },
        etcEtc     : function () { etcEtc(); }
    };
}(jQuery));

このオブジェクトを利用する jQuery プラグインを作成する方法を呼び出すメソッドを強くお勧めします。

$.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');
    }
};

$('whatever').pluginName('methodhere');これですべてが分離され、 etcを実行してモジュールを呼び出します

于 2013-03-05T15:23:50.903 に答える