dojo 官方翻译 dojo/aspect

本文详细介绍了Dojo框架中的切面编程技术,包括after、before和around三种方法的使用方式,展示了如何在目标方法前后插入自定义逻辑,实现对已有功能的增强或修改。

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

官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html

after()

定义:after(target, methodName, advisingFunction, receiveArguments);

意义:在target的methodName方法之后执行advisingFunction方法。

define(["dojo/aspect"], function(aspect){
  aspect.after(dojo, "xhr", function(deferred){
    // this is called after any dojo.xhr call
  });
  // this will execute the original dojo.xhr method and then our advising function
  dojo.xhr("GET", {...});
});
dojo.require("dojo.aspect");
dojo.aspect.after(dojo, "xhr", function(response){
  ...
});

before()

定义:before(target, methodName, advisingFunction);

意义:在target的methodName方法之前执行adviseingFunction。

define(["dojo/aspect"], function(aspect){
  aspect.before(dojo, "xhr", function(method, args){
    // this is called before any dojo.xhr call
    if(method == "PUT"){
      // if the method is PUT, change it to a POST and put the method in the parameter string
      args.url += "?x-method=PUT";
      // return the new args
      return ["POST", args];
    }
  });
  // this will execute the original our advising function and then dojo.xhr
  dojo.xhr("PUT", {...});
});

around()

定义:around(target, methodName, advisingFactory);

define(["dojo/aspect"], function(aspect){
  aspect.around(dojo, "xhr", function(originalXhr){
    return function(method, args){
      // doing something before the original call
      var deferred = originalXhr(method, args);
      // doing something after the original call
      return deferred;
    }
  });
  dojo.xhr("PUT", {...});
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值