当所有websql查询完成后,如何返回自动完成响应回调

时间:2019-03-07 06:14:47

标签: promise async-await web-sql

我正在尝试将脱机功能添加到我的Web应用程序中。它具有自动完成功能,可以从ajax url获取其来源。在几个sql查询后生成Ajax响应。这个工作很好。

现在我想离线制作。为此,我使用Websql使数据库脱机(尽管已弃用),并设法与服务器同步。

要使其脱机,我需要从websql生成自动完成的源。 这是我的脚本示例

$("#add_item").autocomplete({
source: function (request, response) {
    if (!$('#poscustomer').val()) {
        $('#add_item').val('').removeClass('ui-autocomplete-loading');
        bootbox.alert('<?=lang('
        select_above
        ');?>'
    )
        ;
        $('#add_item').focus();
        return false;
    }
    var analyzed = analyze_term(request.term);
    var term = analyzed.term,
        option_id = analyzed.option_id,
        warehouse_id = $("#poswarehouse").val(),
        customer_id = $("#poscustomer").val(),
        limit = 5;
    var getProducts = new Promise(function (resolve, reject) {
        var query = "SELECT products.*, FWP.quantity as quantity, categories.id as category_id, categories.name as category_name FROM `products` LEFT JOIN ( SELECT product_id, warehouse_id, quantity as quantity from warehouses_products ) FWP ON `FWP`.`product_id`=`products`.`id` LEFT JOIN `categories` ON `categories`.`id`=`products`.`category_id` WHERE (`products`.`track_quantity` =0 OR `FWP`.`quantity` >0) AND `FWP`.`warehouse_id` = '" + warehouse_id + "' AND (`products`.`name` LIKE '%" + term + "%' OR `products`.`code` LIKE '%" + term + "%' OR products.name || ' (' || products.code || ')' LIKE '%" + term + "%') GROUP BY `products`.`id` LIMIT " + limit;
        var db = WebSQL('Sync');
        db.query(query, []
        ).fail(function (tx, err) {
            throw new Error(err.message);
        }).done(function (products) {
            var ne = products.map(function (value, index) {
                var data = {};
                value.quantity = 0;
                value.item_tax_method = value.tax_method;
                value.qty = 1;
                value.discount = 0;
                value.serial = '';
                var options = new Promise(function (resolve, reject) {
                    db.query("SELECT product_variants.id as id, product_variants.name as name, product_variants.price as price, product_variants.quantity as total_quantity, FWPV.quantity as quantity FROM `product_variants` LEFT JOIN ( SELECT option_id, warehouse_id, quantity from warehouses_products_variants WHERE product_id = " + value.id + ") FWPV ON `FWPV`.`option_id`=`product_variants`.`id` WHERE `product_variants`.`product_id` = '1' AND `FWPV`.`warehouse_id` = '" + warehouse_id + "' AND `FWPV`.`quantity` > 0 GROUP BY `product_variants`.`id`", []
                    ).fail(function (tx, err) {
                        throw new Error(err.message);
                    }).done(function (array) {
                        resolve(array)
                    });
                });
                var opt;
                options.then(function (options) {
                    if (options.length > 0) {

                        if (option_id && i === 0) {
                            optionById(option_id).then(function (options) {
                                opt = options;
                            })
                        } else {
                            opt = options[0];
                        }
                        if (!option_id || i > 0) {
                            option_id = opt.id;
                        }
                    } else {
                        opt = {
                            price: 0
                        };
                        option_id = false;
                    }
                    value.option = option_id;
                    data.options = opt;
                });
                data.id = makeid(20);
                data.item_id = value.id;
                data.label = value.name + " (" + value.code + ") ";
                data.category = value.category_id;
                data.row = value;
                return data;
            });
            console.log(ne);
            resolve(ne);
        });
    });
    Promise.all([getProducts]).then(function (values) {
        console.log(values);
        response(values[0]);
    });
},
minLength: 1,
autoFocus: false,
delay: 250,
response: function (event, ui) {
    console.log(ui);
    if ($(this).val().length >= 16 && ui.content[0].id == 0) {
        bootbox.alert('No match found', function () {
            $('#add_item').focus();
        }
    )
        ;
        $(this).val('');
    }
    else if (ui.content.length == 1 && ui.content[0].id != 0) {
        ui.item = ui.content[0];
        $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
        $(this).autocomplete('close');
    }
    else if (ui.content.length == 1 && ui.content[0].id == 0) {
        bootbox.alert('no_match_found', function () {
            $('#add_item').focus();
        }
    )
        ;
        $(this).val('');

    }
},
    select: function (event, ui) {
        event.preventDefault();
        if (ui.item.id !== 0) {
            var row = add_invoice_item(ui.item);
            if (row)
                $(this).val('');
        } else {
            bootbox.alert('no_match_found ');
        }
    }
});

以上代码示例部分起作用。在这里,我在map函数中使用了另一个Promise。在那个承诺中,将一个称为“选项”的属性添加到数据对象,并且此map函数返回数据对象的数组。当我在控制台中记录此数据时,我得到了'option'属性。通过在函数response()中,我没有得到这个'option property property

我该如何解决。据我所知,我尝试了许多方法,但结果始终相同。然后我找到了一个npm软件包Web sql promisified,但由于需要在客户端运行而无法使用。感谢您的帮助。

0 个答案:

没有答案