2017-04-11 1 views
1

私はRequire.jsモジュール(AMD)で関数とjQuery関数を定義し、それらをエクスポートしたいと思います。関数をエクスポートするのは簡単ですが、jQuery関数をエクスポートする方法はありますか?ありがとう!Require.jsでカスタムjQuery関数をエクスポートするには?

// filename: someModule.js 
define(function (require, exports) { 

    function funcA() {} 

    $.fn.someFunc = function(callback) { 
     return this.someFunc1(xxx, callback); 
    }; 
    $.fn.extend({ 
     someFunc2: function() {} 
    }); 
    exports.funcA = funcA; 
    // how to export $.fn.someFunc, so I can use it in another module? 
    // and how to export someFunc2? 

}); 

私はこのように、このファイルを使用しています:あなたが持っている何

<script type="text/javascript"> 
    require.config({ 
     baseUrl: '/asset', 
     packages: [ 
     ... 
     ] 
    }); 
    require(['path/to/main.js'], function (main) { 
     main.init(); 
    }); 
</script> 

答えて

0

は、jQueryのプラグインです

// another file: main.js 
define(function (require, exports) { 
    // this module is loaded by require.js 
    var someModule = require('path/to/someModule'); 
    function init() {} 
    exports.init = init; 
}); 

をして、このファイルがサーバーによって.ejsファイルによってレンダリングされます。他のコードでjQueryプラグインを消耗品にするには、$.fn.someFuncと設定すると、すべてとなります。それを使用する必要のあるコードは、それを依存関係としてリストしなければなりません。次のようなものがあります。

define(["jquery", "someFuncModule"], function ($) { 
    $("some selector").someFunc(...); 
}); 

AMDモジュールからさらにエクスポートする必要はありません。

+0

申し訳ありません。私は私のプロジェクトの構造のいくつかの詳細を省略したので、質問を更新しました。 'ejs'ファイルを変更するのではなく、main.jsでsomeModule.jsを要求することはできますか?ありがとう。 – cmal

関連する問題