JQuery推迟解决其他延迟

时间:2014-02-14 15:06:25

标签: javascript jquery jquery-deferred deferred

我有一个函数可以推送其他函数的promises,这些函数也解析了promises数组。我只是想知道这段代码是否正常。

这是主要功能

    /// <summary>Process the validation results (invalid IDR records will have the iop=4)</summary>
this.processValidationResults = function ()
{
    var promises = [];
    var count = (self.idrValidationList.length - 1);

    $.each(self.idrValidationList, function (index, idrValidationItem)
    {
        _onProgress(self.utils.formatCounterMessage(index, count, 'Processing Validation Items'));

        if (idrValidationItem.is_valid = 0)
        {
            //there is a problem with this IDR record
            //update the idr_insp table
            promises.push(self.updateInvalidEntity(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
            promises.push(self.updateInvalidChildren(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
        }
        else
        {
            //push resolved promise
            promise.push($.when());
        }     
    });

    return ($.when.apply($, promises));
}

以下是上述函数

调用的函数
/// <summary>Update the invalid record, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="tabletId" type="Int">Primary Key on the tablet to change</param>
this.updateInvalidEnity = function (entity, tabletId)
{
    //update the record with the new ID and IOP status
    var updateSql = 'UPDATE ' + entity.name + ' SET  iop=? WHERE ' + entity.key_field + '=?';

    //update the record
    return (self.db.executeSql(updateSql, [4, tabletId]));
}

/// <summary>Update the invalid child records, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="keyId" type="Int">Foreign Key on the tablet to change</param>
this.updateInvalidChildren= function (parentEntity, keyId)
{
    var promises = [];
    $.each(parentEntity.child_entities, function (index, child)
    {
        var def = new $.Deferred();
        var updateSql = 'UPDATE ' + child.child_name + ' SET  iop=? WHERE ' + child.key_field + '=?';

        promises.push(self.db.executeSql(updateSql, [4, keyId]));
    });

    return ($.when.apply($, promises));
}

以上所有方法都推迟了下面的推迟。

/* Executes the sql statement with the parameters provided and returns a deffered jquery object */
this.executeSql = function (sql, params)
{
    params = params || [];

    var def = new $.Deferred();

    self.db.transaction(function (tx)
    {
        tx.executeSql(sql, params, function (itx, results)// On Success
        {
            // Resolve with the results and the transaction.
            def.resolve(itx, results);
        },
        function (etx, err)// On Error
        {
            // Reject with the error and the transaction.
            def.reject(etx, err);
        });
    });

    return (def.promise());
}

链条有声吗?尚未测试但我认为没关系。在我继续之前,只想要另外一些眼睛......

1 个答案:

答案 0 :(得分:1)

这应该在Code Review,而不是SO,但是这里......

一些观察结果:

  • 如上所述,.processValidationResults()中的进度消息表示已发出请求,而非收到回复。因此它将直接跳到“计数”。

  • .processValidationResults()中,else { promise.push($.when()); }是不必要的。

  • .updateInvalidChildren()中,var def = $.Deferred()是不必要的。

  • jQuery Deferred的resolvereject方法是“可拆卸的”。在this.executeSql()中,您只想传递参数,就可以简化tx.executeSql(...)tx.executeSql(sql, params, def.resolve, def.reject)的调用。

  • 假设self.db.executeSql执行AJAX(并且tx.executeSql()没有以某种方式对请求进行排队),代码有可能在同时请求的情况下严重打击服务器。确保它可以处理此代码将生成的同时请求的数量。否则,采取措施使请求按顺序进行。