[关闭]
@xiaoyixy 2015-12-15T15:10:40.000000Z 字数 1036 阅读 1385

对延迟对象进行操作

Hybrid


1、处理延迟对象
    延迟对象的用途是允许成功的回调操作在请求成功的时候被调用。延迟对象通过 .resolve( ) 函数可以用与调用成功的回调函数。.resolve( ) 函数可用于为回调函数提供与请求进行交互的参数。
    var deferred = new Deferred();
    // register the success callback with two args
    deferred.done(function(arg1, arg2) {
    alert("Success callback with two artifacts");
    };
    // Do some processing resulting in artifact1 and artifact2
    .
    .
    .
    // Calling the resolve function on the Deferred Object with two artifacts as
    // arguments will trigger the success callback to be called with the same.
    deferred.resolve(artifact1, artifact2);
2、拒绝延迟对象
    延迟对象另一个重要的用途是告知相关对象异步请求失败。延迟对象可通过调用 .reject( ) 函数来调用失败回调操作。与成功的回调相似,.reject( ) 函数可为失败回调操作提供带有描述导致失败的错误提示代码和错误信息。
    var deferred = new Deferred();
    // register the failure callback with the errorCode and errorMsg args
    deferred.fail(function(errorCode, errorMsg) {
    alert("Failure callback: " +errorCode+ " & message" + errorMsg);
    };
    // Do some processing resulting in an error with errCode and corresponding
    // error message errMsg
    .
    .
    // Calling the reject function on the Deferred Object with the error code
    // and err message would be passed back to the callback function(s) that
    // have been registered.
    deferred.reject(errCode, errMsg);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注