【发布时间】:2012-11-21 19:00:50
【问题描述】:
简要说明
这并不像听起来那么基本,所以在你阅读并理解我想要做的事情之前,请不要跳到回答:-)。
我有一个名为 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 对象中,无法检测它们是否已加载。
例如,如果其中一个库包含一个名为 setDate() 的函数插件,我运行:
SM.setDate()
这不一定有效,因为 setDate() 函数可能尚未加载到 SineMacula 对象中,因此它将返回“未捕获的 TypeError...”。
任何人都可以建议对SineMacula.ready() 函数进行良好的调整以检测库是否存在?
请不要对jQuery.load()函数提出建议,我很清楚。
我不希望解决方案是函数load() 的回调,除非绝对必要。
我希望这是有道理的,如果不让我知道,我会发布更多信息。希望尽可能简短。
提前致谢
更新
忘了提到我有一个测试页面here,您可以在其中看到我遇到的错误并更好地了解我在做什么:-)
【问题讨论】:
-
@AbdullahJibaly 我愿意 :-) 如果您想在测试页面上查看错误,请转到 libraries.sinemaculammviii.com/test.php
-
为什么要使用操作 DOM 来创建脚本元素而不是使用 jQuery?
-
@Rob 只是因为 jQuery 在 script 标签中添加了一些奇怪的属性。想要恢复到 jQuery,但不确定属性做了什么。类似
async
标签: javascript jquery sinemacula