jQuery deffered resolve promise

时间:2016-03-11 06:18:52

标签: jquery promise wrapper web-sql deferred

每当我尝试对动态生成的内容进行一些异步调用时,我就会一直走到墙上。 (我还在学习

这次我使用html5sql.js插件,我在下面的代码中运行几个调用。 当我看到我的结果在另一个函数的console.log中返回时,我以为我终于设法了如何使用promises。 然后我花了一天时间弄清楚为什么我无法从返回的结果中访问我的对象...... HUH? 所以我不得不使用旧的糟糕的练习技巧" setTimeout",但我不再那么想了......所以让我们来到我试图做正确的代码。 ...

//We going to fetch all products from the basket corresponding the customers_id.
//The returned array will hold all products data and if the product has attributes it will be returned inside the array.
function restore_contents() {

    var content = $.Deferred();

    var restored_contents = {}; 


    html5sql.process(
        [
            {
                "sql": "SELECT products_id, customers_basket_quantity FROM customers_basket WHERE customers_id = ? ",
                "data": [1],
                "success": function(transaction, results, rows){
                    $.each(rows, function(index, row){
                        if(typeof restored_contents[row.products_id] == 'undefined'){
                            restored_contents[row.products_id] = {};
                        }
                        restored_contents[row.products_id] = {qty : row.customers_basket_quantity, attributes : {}};
                    });

                    //console.log(products_array);
                    populateAttributesArray();
                }
            }                   

        ]
    );          


    function populateAttributesArray(){

        $.each(restored_contents, function(products_id){

            product_id = tep_get_prid(products_id);

            html5sql.process(
                [
                    {
                        "sql": "select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_weight, p.products_tax_class_id from products p, products_description pd where p.products_id = ? and pd.products_id = p.products_id and pd.language_id = ?",
                        "data": [product_id, 1],
                        "success": function(transaction, results, productsData){

                            restored_contents[products_id]['products'] = productsData[0];


                        }                       
                    },
                    {
                        "sql": "SELECT products_options_id, products_options_value_id FROM customers_basket_attributes WHERE customers_id = ? and products_id = ?",
                        "data": [1, products_id],
                        "success": function(transaction, results, attributesData){

                            $.each(attributesData, function(index, attributes){
                                //  console.log(attributes);
                                restored_contents[products_id]['attributes'][attributes.products_options_id] = attributes.products_options_value_id;

                            });

                        }
                    }
                ]
            );//End html5sql.process

        });//End each           


        content.resolve(restored_contents);


    }

    return content.promise();

}

然后在另一个函数中运行上面的代码:

function showCart() {

    $.when( restore_contents() ).done(function(content) {
        console.log( restore_contents ); //All content shows

        $.each(content, function(index, value){ 
            //some code but objects returned not accessible????
        });
   }); //End $.when
}

我试图在网上找到我的特定问题的答案。 我只是无法弄清楚如何为我的目的正确编写它。我只是理解了一点。

0 个答案:

没有答案