I'm using the JQuery UI autocomplete for quite a few text fields on my Backbone.js project. The libraries are loaded in the correct order as follows,
我在Backbone.js项目中的很多文本字段中使用了JQuery UI自动完成功能。库按正确顺序加载如下,
define(['jquery','jquery-ui.min','jquery.ui.touch-punch']);
The autocomplete function gets called, like so:
调用自动完成功能,如下所示:
$("#channelRspm").autocomplete({
minLength: 3,
delay: 1000,
source: function(request, response) {
var results = $.ui.autocomplete.filter(channel, request.term);
response(results.slice(0, 10));
}
});
The problem is that the autocomplete behavior is very random. When running the source on Chrome browser, sometimes it works flawlessly. However, sometimes I get the error in console :
Uncaught TypeError: $(...).autocomplete is not a function
问题是自动完成行为是非常随机的。在Chrome浏览器上运行源代码时,有时它可以完美运行。但是,有时我在控制台中收到错误:Uncaught TypeError:$(...)。autocomplete不是函数
Refreshing the html normally makes it work again but then, that is not what I want. Could anyone please suggest where I'm going worng?
刷新html通常会让它再次起作用,但那时,这不是我想要的。有人可以建议我去哪儿吗?
1 个解决方案
#1
0
You should make sure jquery-ui is loaded after jquery using shim
.
你应该确保使用shim在jquery之后加载jquery-ui。
require.config({
paths: {
"jquery": "lib/jquery",
"jquery-ui": "lib/jquery-ui",
"underscore": "lib/underscore",
"backbone": "lib/backbone"
},
shim: {
"underscore": {
exports: "_"
},
"backbone": {
exports: "Backbone",
deps: ["underscore", "jquery"]
},
"jquery-ui": {
exports: "$",
deps: ['jquery']
}
}
});
You need to specify jquery-ui
as dependency for jquery.ui.touch-punch
in similar way.
您需要以类似的方式将jquery-ui指定为jquery.ui.touch-punch的依赖项。
then define your module like
然后定义你的模块
define(['jquery','jquery-ui','jquery.ui.touch-punch']);
#1
0
You should make sure jquery-ui is loaded after jquery using shim
.
你应该确保使用shim在jquery之后加载jquery-ui。
require.config({
paths: {
"jquery": "lib/jquery",
"jquery-ui": "lib/jquery-ui",
"underscore": "lib/underscore",
"backbone": "lib/backbone"
},
shim: {
"underscore": {
exports: "_"
},
"backbone": {
exports: "Backbone",
deps: ["underscore", "jquery"]
},
"jquery-ui": {
exports: "$",
deps: ['jquery']
}
}
});
You need to specify jquery-ui
as dependency for jquery.ui.touch-punch
in similar way.
您需要以类似的方式将jquery-ui指定为jquery.ui.touch-punch的依赖项。
then define your module like
然后定义你的模块
define(['jquery','jquery-ui','jquery.ui.touch-punch']);