深入分析DWREngine.endBatch


DWREngine.endBatch将在DWREngine._execute最后被调用

--〉DWREngine._execute--〉DWREngine.endBatch)

/**

* Finished grouping a set of remote calls together. Go and execute them all.

* @see http://getahead.ltd.uk/dwr/browser/engine/batch

*/

//函数定义

DWREngine.endBatch = function(options) {

//设置请求对象

var batch = DWREngine._batch;

//请求对象为空返回

if (batch == null) {

DWREngine._handleError("No batch in progress.");

return;

}

// Merge the global batch level properties into the batch meta data

//如果形参存在那么有如下两步,通常行参是不存在的, 在DWREngine._execute中是如此调用endBatch的:DWREngine.endBatch();


if (options && options.preHook) batch.preHooks.unshift(options.preHook);

if (options && options.postHook) batch.postHooks.push(options.postHook);

//除非你写程序时调用过DWREngine.setPreHook,否则下面代码无用

if (DWREngine._preHook) batch.preHooks.unshift(DWREngine._preHook);

//除非你写程序时调用过DWREngine.setPostHook,否则下面代码无用

if (DWREngine._postHook) batch.postHooks.push(DWREngine._postHook);

//除非你手工改过源代码,否则下面的4个判断条件肯定为真

//DWREngine._method默认为1,表示采用XMLHttpRequest,除非你写程序时调用过DWREngine.setMethod

if (batch.method == null) batch.method = DWREngine._method;

//DWREngine._verb默认为"post",除非你写程序时调用过DWREngine.setVerb

if (batch.verb == null) batch.verb = DWREngine._verb;

//DWREngine._async默认为true,表示请求是异步的,除非你写程序时调用过DWREngine.setAsync

if (batch.async == null) batch.async = DWREngine._async;

//DWREngine.timeout 默认为0,除非你写程序时调用过DWREngine.setTimeout,

if (batch.timeout == null) batch.timeout = DWREngine._timeout;

//标记请求未完成

batch.completed = false;

// We are about to send so this batch should not be globally visible

//将DWREngine._batch赋值给临时变量batch后,将DWREngine._batch设为空

DWREngine._batch = null;

// If we are in ordered mode, then we don't send unless the list of sent

// items is empty

//判断DWREngine._ordered,默认为false

if (!DWREngine._ordered) {

DWREngine._sendData(batch);

//将请求对象放入请求数组,暂时没看出有什么用

DWREngine._batches[DWREngine._batches.length] = batch;

}

//如果通过调用DWREngine.setOrdered设置了_ordered,那么会使用_batchQueue,且进入下面的else块,否则不用理会下面的代码

else {

if (DWREngine._batches.length == 0) {

// We aren't waiting for anything, go now.

DWREngine._sendData(batch);

DWREngine._batches[DWREngine._batches.length] = batch;

}

else {

// Push the batch onto the waiting queue

DWREngine._batchQueue[DWREngine._batchQueue.length] = batch;

}

}

};

总结:

此函数主要是设置请求对象属性:_preHooks,postHooks,method,verb ,async ,timeout

然后调用DWREngine._sendData(batch);最后将请求加入数组或队列

你可能感兴趣的:(DWR,Go)