1

我有一个页面,我在其中通过对数据库的两次异步调用创建了一个客户(公司)及其联系人。联系人应该在客户之后保存,因为它需要包含在服务器响应中的客户 ID。此外,在完成所有操作后,还会重定向到相应的新客户页面。

照原样,重定向甚至在保存任何联系人之前发生。我尝试使用 jQuery 的$.when()and .done(),但它可能不起作用,因为.when()和 回调之间存在断开连接。我怎样才能使它在这种情况下工作?

function saveAll() {
    createCustomer();
    createContacts();
    var url = "<newCustomerDetailPage>";    // redirect when all is done
    window.location = url;
}


function callback(result) {
    if (result) {
        // do something
    } else {
        // do something else
    }
}


function errorHandler(error) {
    // do something
}


function createCustomer() {       // Create 1 new customer
    // prep fields
    var client = new toolkit.Client();
    client.create(<fields>, callback, errorHandler);
}


function createContacts() {       // Create 1 or more new contacts
    $('.row').each(function() {
        // prep fields
        createContact(fields);
    }
}


function createContact(fields) {      // Create 1 new contact
    var client = new toolkit.Client();
    client.create(<fields>, callback, errorHandler);
}


function handleResult(result, callback, errorHandler) {
    if (blah...)
        callback(result)
    else
        errorHandler(error)
}

工具包.客户端:

toolkit.Client = function(){}

toolkit.Client.prototype.create = function(fields, callback, errorHandler) {
    // async call to database 
    function(result){        // handle server response
        handleResult(result, callback, errorHandler);
    }
}

请注意,client.create一次只能保存一条记录。

已经试过了:

var actions = [];

function createContacts() {       // Create 1 or more new contacts
    $('.row').each(function() {
        // prep fields
        actions.push(function(){createContact(fields);});
    }
}

function saveAll() {
    createCustomer();
    createContacts();
    $.when.apply($,actions).done(function(){
        var url = "<newCustomerDetailPage>";    // redirect when all is done
        window.location = url;
    });
}
4

2 回答 2

1

您可以使用以下命令等待每个循环的结束promise

var actions = [];

function createContacts(callback) {       // Create 1 or more new contacts
    $('.row').each(function() {
        // prep fields
        actions.push(function(){createContact(fields);});
    }.promise().done(callback);
}

function saveAll() {
    createCustomer();
    createContacts(function(){
        $.when.apply($,actions).done(function(){
            var url = "<newCustomerDetailPage>";    // redirect when all is done
            window.location = url;
        });
    });

}
于 2013-06-09T13:17:22.623 回答
0

理想情况下,您client.create应该返回一个延迟对象,因为他是唯一一种负责创建联系人的方法,并且他是唯一可以解决它的方法:

toolkit.Client.prototype.create = function(fields, callback, errorHandler) {
    var def = $.Deferred();
    // async call to database 

    //resolve def when the result is ready
    //def.resolve or def.refect();

    return def;
}

createContact还必须返回结果:

function createContact(fields) {      // Create 1 new contact
    var client = new toolkit.Client();
    return client.create(<fields>, callback, errorHandler);
}

之后你可以这样做:

function createContacts() {       // Create 1 or more new contacts
    var actions = [];
    $('.row').each(function() {
        // prep fields
        actions.push(createContact(fields));
    }

    return actions;
}

function saveAll() {
    createCustomer();
    $.when.apply($, createContacts()).done(function(){
        var url = "<newCustomerDetailPage>";    // redirect when all is done
        window.location = url;
    });
}
于 2013-06-09T13:32:56.023 回答