1

簡単な説明

これは思ったほど基本的ではないので、私がやろうとしていることを読んで理解する前に、答えにスキップしないでください:-)。

SineMacula私はそのようないくつかの基本関数を含むと呼ばれるオブジェクトを持っています(ready今のところ関数を無視してください):

(function(global, $){

    // MOST CODE STRIPT OUT HERE */

    /**
     * Sine Macula Run
     * Makes it easy to write plugins for the Sine Macula library
     *
     * @param function callback
     */
    SM.run = run;
    function run(callback){
        // Call the function with the Sine Macula
        // and jQuery objects
        callback(SM, $);
    }

    /**
     * Sine Macula Ready
     * Executes code once the Sine Macula and jQuery
     * libraries are ready
     *
     * @param function callback
     */
    SM.ready = ready;
    function ready(callback){
        // Call the function with the Sine Macula
        // and jQuery objects
        jQuery(function($) {
            callback(SM, $);
        });
    }

    /**
     * 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 parameter The options for the Sine Macula load
     */
    SM.load = load;
    function load(options){
        // BUILD A QUERY HERE
        // ...
        // 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);       
    }
})(this, this.jQuery);

このload()関数は、ページのに関連するスクリプトタグを追加することにより、ページに関連するすべてのプラグイン/ライブラリをロードするだけheadです。

ロードされたページのすべてのスクリプトは、と呼ばれる関数へのコールバックとして実行されますrun。これにより、jQuerySineMaculaが両方ともプラグインに渡されることが保証されます。

問題

SineMaculaライブラリがオブジェクトにロードされているため、ライブラリがロードされているかどうかを検出する方法がないため、ここに問題があります。

たとえば、ライブラリの1つに、という関数プラグインが含まれている場合、次のコマンドsetDate()を実行します。

SM.setDate()

setDate()関数がまだオブジェクトにロードされていない可能性があるため、これは必ずしも機能するとは限りませSineMaculaん。したがって、「UncaughtTypeError...」が返されます。

SineMacula.ready()ライブラリが存在するかどうかを検出するための関数への適切な適応について誰かがアドバイスできますか?

この機能については提案しないでくださいjQuery.load()。私はそれをよく知っています。

load()どうしても必要な場合を除いて、ソリューションを関数のコールバックにしたくありません。

私はこれが理にかなっていることを願っています、そうでなければ私に知らせてください、そして私はより多くの情報を投稿します。できるだけ短くしたかった。

前もって感謝します

アップデート

私がここにテストページを持っていることを言及するのを忘れました。そこでは、私が得ているエラーを見て、私がしていることをよりよく理解することができます:-)

4

2 に答える 2

1

私が正しく理解していればSM.run()、スクリプト内から呼び出したユーザーSM.load()が、ライブラリの他の部分をロードするを呼び出します。

したがって、その場合は、ライブラリの他の部分をロードする前に、スクリプトの実行が終了します。

.run()あなたがする必要があるのは、ユーザーが残りのコードとは別のスクリプトにそれらを持っているという要件があることだと思います。document.write次に、他のスクリプトをロードするために使用できます。これにより、ユーザーの残りのコードを含む次のスクリプトが読み込まれ、ブロックされます。

function load(options){
    // BUILD A QUERY HERE
    // ...
    // Complete the url by appending the query
    document.write('<scr' + 'ipt type="text/javascript" ',
                            ' src="//libraries.sinemaculammviii.com/' + query,
                            '"><\/scr' + 'ipt>');      
}

<script type="text/javascript" src="/path/to/your/lib.js"></script>
<script type="text/javascript">
    SineMacula.load('all');
</script>

<!-- The document.write will write the new script here, and it will be loaded
         syncronously, so it will block. -->

<script type="text/javascript">
    // code that uses the loaded library parts
</script>
于 2012-11-21T19:18:22.180 に答える
1

いくつかのコンテキストが欠落しているため、私があなたの質問に答えているかどうかはわかりませんが、Deferredsを使用することをお勧めします:

SM.ready = ready;
function ready(callback){
    var deferred = $.Deferred();
    // Call the function with the Sine Macula
    // and jQuery objects
    jQuery(function($) {
        deferred.resolve();
        if (typeof(callback) === "function") {
            callback(SM, $);
        }
    });
    return deferred;
}

今、あなたは次のようなことをすることができます:

SM.ready().done(function() {
    // everything is now set.
});
于 2012-11-21T19:19:54.860 に答える