2

多くの人が知っているように、Googleは、単純な関数を呼び出すことで特定のモジュール/ライブラリをロードできるAPIを提供しています。

<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("jquery", "1.7.1");
    google.load("jqueryui", "1.8.2");
</script>

私は現在、自分のWebサイトに簡単かつ効率的に配布したいコードの独自のライブラリを開発中であり、上記の方法よりも優れた方法は考えられません。

ただし、このためのコードを作成するための最良の方法が何であるかはよくわかりません。明らかに私のライブラリはオブジェクトになるので、次のようなものから始めます(私が間違っている場合は、私を修正すると思います):

function company(){
    this.load = function(modules){
        // Modules is an array of modules to load
        // Load the separate modules here
        // from external files in their minified format
    }
}

var company = new company;
company.load(['forms']);

上記の方法は正しい方法ですか?次に、別々のファイルからモジュールをロードするにはどうすればよいですか?

4

2 に答える 2

0

CommonJSモジュール仕様を使用できます。次に、RequireJSを使用してそれらをロードできます。

于 2012-10-17T08:06:03.860 に答える
0

周りを探して他の人から経験を積んだ後、私は次のことが最良の方法であると思いました。他の人は同意しないかもしれません、コメントしてください

/**
 * Sine Macula Javascript API
 * The Sine Macula API contains all base functions for use throughout
 * all websites
 * @name class.sinemacula.js
 * @author Ben Carey
 * @version 1.0
 * @date 25/10/2012
 * @copyright (c) 2012 Sine Macula Limited (sinemaculammviii.com)
 */

function SineMacula(){

    // Only proceed if jQuery has been loaded
    if(typeof jQuery=='undefined'){
        // jQuery has not been loaded
        console.log('jQuery has not been loaded');
    }else{

        /**
         * Sine Macula Load
         * Load the Sine Macula Libraries and Plugins
         * into the current document
         *
         * The options:
         * - package: the package of libraries to load
         * - packageURL: a remote source to load the package details from
         * - libraries: any additional libraries to load
         *
         * @param object options The options for the Sine Macula load
         */
        this.load = function(options){
            var url,query,script;
            // Set the defaults for the loader
            var options = $.extend({
                package:    'none', // Do not load any packages by default
                packageURL: false, // Do not retrieve the package details from a URL by default
                libraries:  [] // Do not load any libraries by default
            },options);
            // Build the query based on the parameters supplied
            if(options.packageURL){
                // Build the query to allow for a remote
                // package definition
                query = '?packageURL='+encodeURIComponent(options.packageURL);
            }else if(options.package=='none'){
                query = '?libraries='+encodeURIComponent(options.libraries.join());
            }else{
                query = encodeURIComponent(options.package)+'/?libraries='+encodeURIComponent(options.libraries.join());
            }
            // Complete the url by appending the query
            url = '//libraries.sinemaculammviii.com/'+query;
            // Append the script tag to the end of the document
            script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = url;
            $('head')[0].appendChild(script);
        }
    }
};
于 2012-11-05T15:06:28.787 に答える