DWR 3.0版本:
1.传递额外的参数
Rules:
- arg - If specified will be used as a default argument to pass to the callback and will apply to both callback handlers and exception handlers. 如果设置该参数则会作为一个默认的参数传递给回调函数和错误处理函数
- callbackArg - If specified will apply to callback handlers and will override arg.如果设置该参数则会传递给回调函数,会覆盖arg的设置
- exceptionArg - If specified will apply to exception handlers and will override arg.如果设置该参数则会传递给错误处理函数,会覆盖arg的设置
示例:
var dataFromBrowser = ...; var dataFromBrowser2 = ...; var callMetaData = { callback:callbackFunction, arg: dataFromBrowser, // specify an argument to pass to the exeptionHandler callbackArg: dataFromBrowser2, // overrides args will be passed to the callback exceptionHandler: exceptionHandlerFunction }; Remote.method(params, callMetaData); function callbackFunction(dataFromServer, arg1) { // you will now have access to dataFromBrowser2 as arg1 // callbackArg overrides arg } function exceptionHandlerFunction(exceptionMessage, exception, arg1) { // you will now have access to dataFromBrowser1 as arg1 // arg also applies to exceptionHandlers }
2.作用域
Rules:
- scope - If specified will be used as the default scope and will apply to both callback handlers and exception handlers.回调函数和错误处理函数的作用域
- callbackScope - If specified will apply to callback handlers and will override scope.回调函数的作用域,覆盖scope的设置
- exceptionScope - If specified will apply to exception handlers and will override scope.错误处理函数的作用域 ,覆盖scope的设置
Note: The default scope is window. 默认作用域是window
示例:
(function() { someObject = {}; someObject.privateVar = "Private variable from the main object."; someObject.callbackFunction = function(dataFromServer) { alert(this.privateVar); // The preceding line will alert the value of privateVar. // The key here is the use of 'this'. The scope is not // lost because it is specified in the call-meta data object // and used to execute the callback function. } })(); var callMetaData = { callback:someObject.callbackFunction, scope: someObject }; Remote.method(params, callMetaData);
DWR3.0之前的版本:
用闭包来实现,如下示例:
var dataFromBrowser = ...; // define an erasure function to store a reference to // dataFromBrowser and to call dataFromServer var callbackProxy = function(dataFromServer) { callbackFunc(dataFromServer, dataFromBrowser); }; var callMetaData = { callback:callbackProxy }; Remote.method(params, callMetaData);