DWR 3.0 回调函数新增参数

本文详细介绍了DWR 3.0版本中如何传递额外参数及设置回调函数的作用域,包括arg、callbackArg、exceptionArg等参数的使用规则,以及scope、callbackScope、exceptionScope的作用域设置规则,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

 

 

原文:http://directwebremoting.org/dwr/browser/extra-data.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值