📄 engine.js
字号:
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * Declare a constructor function to which we can add real functions. * @constructor */function DWREngine() { }/** * Set an alternative error handler from the default alert box. * @see http://getahead.ltd.uk/dwr/browser/engine/errors */DWREngine.setErrorHandler = function(handler) { DWREngine._errorHandler = handler;};/** * Set an alternative warning handler from the default alert box. * @see http://getahead.ltd.uk/dwr/browser/engine/errors */DWREngine.setWarningHandler = function(handler) { DWREngine._warningHandler = handler;};/** * Set a default timeout value for all calls. 0 (the default) turns timeouts off. * @see http://getahead.ltd.uk/dwr/browser/engine/errors */DWREngine.setTimeout = function(timeout) { DWREngine._timeout = timeout;};/** * The Pre-Hook is called before any DWR remoting is done. * @see http://getahead.ltd.uk/dwr/browser/engine/hooks */DWREngine.setPreHook = function(handler) { DWREngine._preHook = handler;};/** * The Post-Hook is called after any DWR remoting is done. * @see http://getahead.ltd.uk/dwr/browser/engine/hooks */DWREngine.setPostHook = function(handler) { DWREngine._postHook = handler;};/** XHR remoting method constant. See DWREngine.setMethod() */DWREngine.XMLHttpRequest = 1;/** XHR remoting method constant. See DWREngine.setMethod() */DWREngine.IFrame = 2;/** * Set the preferred remoting method. * @param newmethod One of DWREngine.XMLHttpRequest or DWREngine.IFrame * @see http://getahead.ltd.uk/dwr/browser/engine/options */DWREngine.setMethod = function(newmethod) { if (newmethod != DWREngine.XMLHttpRequest && newmethod != DWREngine.IFrame) { DWREngine._handleError("Remoting method must be one of DWREngine.XMLHttpRequest or DWREngine.IFrame"); return; } DWREngine._method = newmethod;};/** * Which HTTP verb do we use to send results? Must be one of "GET" or "POST". * @see http://getahead.ltd.uk/dwr/browser/engine/options */DWREngine.setVerb = function(verb) { if (verb != "GET" && verb != "POST") { DWREngine._handleError("Remoting verb must be one of GET or POST"); return; } DWREngine._verb = verb;};/** * Ensure that remote calls happen in the order in which they were sent? (Default: false) * @see http://getahead.ltd.uk/dwr/browser/engine/ordering */DWREngine.setOrdered = function(ordered) { DWREngine._ordered = ordered;};/** * Do we ask the XHR object to be asynchronous? (Default: true) * @see http://getahead.ltd.uk/dwr/browser/engine/options */DWREngine.setAsync = function(async) { DWREngine._async = async;};/** * The default message handler. * @see http://getahead.ltd.uk/dwr/browser/engine/errors */DWREngine.defaultMessageHandler = function(message) { if (typeof message == "object" && message.name == "Error" && message.description) { alert("Error: " + message.description); } else { alert(message); }};/** * For reduced latency you can group several remote calls together using a batch. * @see http://getahead.ltd.uk/dwr/browser/engine/batch */DWREngine.beginBatch = function() { if (DWREngine._batch) { DWREngine._handleError("Batch already started."); return; } // Setup a batch DWREngine._batch = {}; DWREngine._batch.map = {}; DWREngine._batch.paramCount = 0; DWREngine._batch.map.callCount = 0; DWREngine._batch.ids = []; DWREngine._batch.preHooks = []; DWREngine._batch.postHooks = [];};/** * 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 if (options && options.preHook) batch.preHooks.unshift(options.preHook); if (options && options.postHook) batch.postHooks.push(options.postHook); if (DWREngine._preHook) batch.preHooks.unshift(DWREngine._preHook); if (DWREngine._postHook) batch.postHooks.push(DWREngine._postHook); if (batch.method == null) batch.method = DWREngine._method; if (batch.verb == null) batch.verb = DWREngine._verb; if (batch.async == null) batch.async = DWREngine._async; 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 = null; // If we are in ordered mode, then we don't send unless the list of sent // items is empty if (!DWREngine._ordered) { DWREngine._sendData(batch); DWREngine._batches[DWREngine._batches.length] = batch; } 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; } }};//==============================================================================// Only private stuff below here//==============================================================================/** A function to call if something fails. */DWREngine._errorHandler = DWREngine.defaultMessageHandler;/** A function to call to alert the user to some breakage. */DWREngine._warningHandler = DWREngine.defaultMessageHandler;/** A function to be called before requests are marshalled. Can be null. */DWREngine._preHook = null;/** A function to be called after replies are received. Can be null. */DWREngine._postHook = null;/** An array of the batches that we have sent and are awaiting a reply on. */DWREngine._batches = [];/** In ordered mode, the array of batches waiting to be sent */DWREngine._batchQueue = [];/** A map of known ids to their handler objects */DWREngine._handlersMap = {};/** What is the default remoting method */DWREngine._method = DWREngine.XMLHttpRequest;/** What is the default remoting verb (ie GET or POST) */DWREngine._verb = "POST";/** Do we attempt to ensure that calls happen in the order in which they were sent? */DWREngine._ordered = false;/** Do we make the calls async? */DWREngine._async = true;/** The current batch (if we are in batch mode) */DWREngine._batch = null;/** The global timeout */DWREngine._timeout = 0;/** ActiveX objects to use when we want to convert an xml string into a DOM object. */DWREngine._DOMDocument = ["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];/** The ActiveX objects to use when we want to do an XMLHttpRequest call. */DWREngine._XMLHTTP = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];/** * @private Send a request. Called by the Javascript interface stub * @param path part of URL after the host and before the exec bit without leading or trailing /s * @param scriptName The class to execute * @param methodName The method on said class to execute * @param func The callback function to which any returned data should be passed * if this is null, any returned data will be ignored * @param vararg_params The parameters to pass to the above class */DWREngine._execute = function(path, scriptName, methodName, vararg_params) { var singleShot = false; if (DWREngine._batch == null) { DWREngine.beginBatch(); singleShot = true; } // To make them easy to manipulate we copy the arguments into an args array var args = []; for (var i = 0; i < arguments.length - 3; i++) { args[i] = arguments[i + 3]; } // All the paths MUST be to the same servlet if (DWREngine._batch.path == null) { DWREngine._batch.path = path; } else { if (DWREngine._batch.path != path) { DWREngine._handleError("Can't batch requests to multiple DWR Servlets."); return; } } // From the other params, work out which is the function (or object with // call meta-data) and which is the call parameters var params; var callData; var firstArg = args[0]; var lastArg = args[args.length - 1]; if (typeof firstArg == "function") { callData = { callback:args.shift() }; params = args; } else if (typeof lastArg == "function") { callData = { callback:args.pop() }; params = args; } else if (typeof lastArg == "object" && lastArg.callback != null && typeof lastArg.callback == "function") { callData = args.pop(); params = args; } else if (firstArg == null) { // This could be a null callback function, but if the last arg is also // null then we can't tell which is the function unless there are only // 2 args, in which case we don't care! if (lastArg == null && args.length > 2) { if (DWREngine._warningHandler) { DWREngine._warningHandler("Ambiguous nulls at start and end of parameter list. Which is the callback function?"); } } callData = { callback:args.shift() }; params = args; } else if (lastArg == null) { callData = { callback:args.pop() }; params = args; } else { if (DWREngine._warningHandler) { DWREngine._warningHandler("Missing callback function or metadata object."); } return; } // Get a unique ID for this call var random = Math.floor(Math.random() * 10001); var id = (random + "_" + new Date().getTime()).toString(); var prefix = "c" + DWREngine._batch.map.callCount + "-"; DWREngine._batch.ids.push(id); // batchMetaData stuff the we allow in callMetaData for convenience if (callData.method != null) { DWREngine._batch.method = callData.method; delete callData.method; } if (callData.verb != null) { DWREngine._batch.verb = callData.verb; delete callData.verb; } if (callData.async != null) { DWREngine._batch.async = callData.async; delete callData.async; } if (callData.timeout != null) { DWREngine._batch.timeout = callData.timeout; delete callData.timeout; } // callMetaData stuff that we handle with the rest of the batchMetaData if (callData.preHook != null) { DWREngine._batch.preHooks.unshift(callData.preHook); delete callData.preHook; } if (callData.postHook != null) { DWREngine._batch.postHooks.push(callData.postHook); delete callData.postHook; } // Default the error and warning handlers if (callData.errorHandler == null) callData.errorHandler = DWREngine._errorHandler; if (callData.warningHandler == null) callData.warningHandler = DWREngine._warningHandler; // Save the callMetaData DWREngine._handlersMap[id] = callData; DWREngine._batch.map[prefix + "scriptName"] = scriptName; DWREngine._batch.map[prefix + "methodName"] = methodName; DWREngine._batch.map[prefix + "id"] = id; // Serialize the parameters into batch.map DWREngine._addSerializeFunctions(); for (i = 0; i < params.length; i++) { DWREngine._serializeAll(DWREngine._batch, [], params[i], prefix + "param" + i); } DWREngine._removeSerializeFunctions();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -