我正在尝试删除一些初始化 typeahead.js 插件的 JQuery 代码,该插件使用 Bloodhound 建议引擎和远程数据源。这是到目前为止的代码:
var locsData = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("name"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "/locations/search.json?q=%QUERY",
ajax: {
beforeSend: function(xhr, settings) {
$("#typeahead1").addClass('loading-text');
},
complete: function(xhr, status) {
$("#typeahead1").removeClass('loading-text');
}
}
},
limit: 100
});
locsData.initialize();
$("#typeahead1").typeahead({
minLength: 3
}, {
name: "locs",
displayKey: "name",
source: locsData.ttAdapter()
});
我正在尝试将这段代码概括为一堆不同的文本字段;这不应该是一个问题:我可以使用$(".myClass").typeahead({ ... })
并且插件将为所有这些控件初始化。我面临的问题是 Bloodhound 配置(、等)中的回调ajax
函数:我正在为这些函数中的文本字段切换“loading.gif”类,我需要该代码来获取参考到调用对象。beforeSend
complete
如何在这些回调中获取对调用文本字段的引用?我需要这样的东西:
$(<text-field reference>).addClass('loading-text');
我希望有人可以帮助我。提前致谢!
编辑- 现在我正在使用以下代码以某种方式解决我的问题,但是关于获取调用对象引用(这个主题)的问题仍然悬而未决。
if ($(document.activeElement).typeahead != null) {
$(document.activeElement).addClass('loading-text');
}