【发布时间】:2014-09-04 03:57:54
【问题描述】:
我正在尝试动态加载 jQuery,然后动态加载 Typeahead.js jQuery 插件。
我可以加载 jQuery,但 typeahead 插件似乎没有加载。代码的第一部分检查 jQuery 并在它不存在时动态加载它。
加载 jQuery 后,我尝试使用 $.getScript 加载 Typeahead,它在 jQuery 文档中用于加载 jQuery 插件。但是,Typehead 插件似乎没有加载,因为当我尝试调用 Bloodhound(Typeahead 中的建议引擎)时出现错误。
我确认下面显示的预输入代码通过将其放入 Rails 应用程序中有效。当与 jquery-rails gem 和资产管道一起使用时,typeahead 代码会正确地使用 JSON 响应填充我的输入框。但是,我不能依赖 Rails,当我尝试分离代码时,我无法以正确的顺序动态加载库。
任何帮助将不胜感激。
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined) {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else { // Other browsers
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
var parentHead = (document.getElementsByTagName("head")[0] || document.documentElement)
parentHead.insertBefore(script_tag, parentHead.firstChild);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Load Typeahead
// Call our main function
main();
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
/******* This is the call to load typeahead *******/
var url = "https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"
$.getScript(url, function(){
var companies = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 3000,
prefetch: {
// url points to a json file that contains an array of country names, see
// https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json
url: '/accounts.json',
}
});
// kicks off the loading/processing of `local` and `prefetch`
companies.initialize();
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#the-basics .search .typeahead').typeahead(null, {
name: 'companies',
displayKey: 'name',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: companies.ttAdapter()
});
});
});
}
})();
【问题讨论】:
-
会发生什么?您是否看到任何错误消息?能否确认
.getScript调用成功完成并调用回调? -
我收到 Bloodhound 的参考错误和 Typeahead.js 源代码中的错误。我确认调用了 $.getScript 方法,因为我在回调中到达了我的调试器,但 Typeahead 似乎没有加载。两个错误:1. Uncaught TypeError: Cannot read property 'isArray' of undefined 2. Uncaught ReferenceError: Bloodhound is not defined
-
isArray错误来自尝试解析加载的预输入代码,并且是该脚本中第一次使用“$”。它表现得好像无法解决window.jQuery。看起来是时候在回调的顶部扔一个断点了,看看window.jQuery的状态是什么。
标签: javascript jquery typeahead.js