簡単な説明
これは思ったほど基本的ではないので、私がやろうとしていることを読んで理解する前に、答えにスキップしないでください:-)。
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
。これにより、jQuery
とSineMacula
が両方ともプラグインに渡されることが保証されます。
問題
SineMacula
ライブラリがオブジェクトにロードされているため、ライブラリがロードされているかどうかを検出する方法がないため、ここに問題があります。
たとえば、ライブラリの1つに、という関数プラグインが含まれている場合、次のコマンドsetDate()
を実行します。
SM.setDate()
setDate()
関数がまだオブジェクトにロードされていない可能性があるため、これは必ずしも機能するとは限りませSineMacula
ん。したがって、「UncaughtTypeError...」が返されます。
SineMacula.ready()
ライブラリが存在するかどうかを検出するための関数への適切な適応について誰かがアドバイスできますか?
この機能については提案しないでくださいjQuery.load()
。私はそれをよく知っています。
load()
どうしても必要な場合を除いて、ソリューションを関数のコールバックにしたくありません。
私はこれが理にかなっていることを願っています、そうでなければ私に知らせてください、そして私はより多くの情報を投稿します。できるだけ短くしたかった。
前もって感謝します
アップデート
私がここにテストページを持っていることを言及するのを忘れました。そこでは、私が得ているエラーを見て、私がしていることをよりよく理解することができます:-)