【问题标题】:jQuery promises with summernote hints带有 Summernote 提示的 jQuery 承诺
【发布时间】:2023-04-06 09:46:02
【问题描述】:

我正在努力从Summernote Hints 的数据库中获取实时用户列表,但是在使用异步时它会崩溃,但是在关闭异步时会导致 UI 冻结...显然不是 UX 的最佳选择。

$(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;
            }
        }
    });
});

我怎样才能让异步工作而不摔倒?我相信这与承诺有关,但不确定。

【问题讨论】:

    标签: javascript jquery ajax summernote


    【解决方案1】:

    不要打电话给callbackusers 需要从 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;
                }
            }
        });
    });
    

    【讨论】:

    • callback 是一个函数,在你的答案中没有。
    • @Ian 我只是将它传递给done 并让done 调用它。
    • 啊,是的,我错过了用户函数中的回调,谢谢!奇迹般有效。再过几分钟我无法接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 2012-12-19
    • 2017-04-02
    • 2014-10-27
    • 1970-01-01
    • 2016-04-02
    • 2020-07-20
    • 2018-11-18
    相关资源
    最近更新 更多