jQuery承诺与summernote提示 [英] jQuery promises with summernote hints

查看:102
本文介绍了jQuery承诺与summernote提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从数据库中获取夏令营提示然而,当使用异步时,它只会失败,但是如果异步关闭它会导致UI冻结......显然不适合用户体验。

I am working on getting a live user list from a database for the Summernote Hints however when using async it just falls over, however with async off it causes the UI to freeze... clearly not optimal for the 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;
            }
        }
    });
});

如何在不摔倒的情况下让异步工作?我相信它与承诺有关,但不确定。

How can I get async to work without falling over? I believe it has something to do with promises however not sure.

推荐答案

不要调用回调用户需要通过 done 函数调用该函数。

Don't call callback. users needs to call that from the done function.

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

这篇关于jQuery承诺与summernote提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆