gpt4 book ai didi

javascript - jQuery promises with summernote hints

转载 作者:行者123 更新时间:2023-11-30 12:07:06 25 4
gpt4 key购买 nike

我正在努力从 Summernote Hints 的数据库中获取实时用户列表然而,当使用异步时,它就会倒下,但是关闭异步时,它会导致 UI 卡住……显然不是用户体验的最佳选择。

$(document).ready(function()
{
$('.editor').summernote({
height: 300,
hint: {
match: /\B@(\w*)$/,
users: function(keyword) {

var result = data;

$.ajax({
url: '/users/' + keyword,
type: 'get',
async: false //This works but freezes the UI
}).done(function(data)
{
result = data; //Set the result to the returned json array
});

return result;
},
search: function (keyword, callback) {
callback(this.users(keyword)); //callback must be an array
},
content: function (item) {
return '@' + item;
}
}
});
});

如何在不摔倒的情况下让 async 工作?我相信这与 promise 有关但不确定。

最佳答案

不要调用回调users 需要从 done 函数中调用它。

$(document).ready(function()
{
$('.editor').summernote({
height: 300,
hint: {
match: /\B@(\w*)$/,
users: function(keyword, callback) {
$.ajax({
url: '/users/' + keyword,
type: 'get',
async: true //This works but freezes the UI
}).done(callback);
},
search: function (keyword, callback) {
this.users(keyword, callback); //callback must be an array
},
content: function (item) {
return '@' + item;
}
}
});
});

关于javascript - jQuery promises with summernote hints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com