aop.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // wrapped by build app
  2. define("dojox/lang/oo/aop", ["dijit","dojo","dojox","dojo/require!dojox/lang/oo/Decorator,dojox/lang/oo/general"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.oo.aop");
  4. dojo.require("dojox.lang.oo.Decorator");
  5. dojo.require("dojox.lang.oo.general");
  6. (function(){
  7. var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general, ooa = oo.aop,
  8. isF = dojo.isFunction;
  9. // five decorators implementing light-weight AOP weaving
  10. /*=====
  11. ooa.before = md(function(name, newValue, oldValue){
  12. // summary: creates a "before" advise, by calling new function
  13. // before the old one
  14. // dummy body
  15. });
  16. ooa.around = md(function(name, newValue, oldValue){
  17. // summary: creates an "around" advise,
  18. // the previous value is passed as a first argument and can be null,
  19. // arguments are passed as a second argument
  20. // dummy body
  21. });
  22. =====*/
  23. // reuse existing decorators
  24. ooa.before = oog.before;
  25. ooa.around = oog.wrap;
  26. ooa.afterReturning = md(function(name, newValue, oldValue){
  27. // summary: creates an "afterReturning" advise,
  28. // the returned value is passed as the only argument
  29. return isF(oldValue) ?
  30. function(){
  31. var ret = oldValue.apply(this, arguments);
  32. newValue.call(this, ret);
  33. return ret;
  34. } : function(){ newValue.call(this); };
  35. });
  36. ooa.afterThrowing = md(function(name, newValue, oldValue){
  37. // summary: creates an "afterThrowing" advise,
  38. // the exception is passed as the only argument
  39. return isF(oldValue) ?
  40. function(){
  41. var ret;
  42. try{
  43. ret = oldValue.apply(this, arguments);
  44. }catch(e){
  45. newValue.call(this, e);
  46. throw e;
  47. }
  48. return ret;
  49. } : oldValue;
  50. });
  51. ooa.after = md(function(name, newValue, oldValue){
  52. // summary: creates an "after" advise,
  53. // it takes no arguments
  54. return isF(oldValue) ?
  55. function(){
  56. var ret;
  57. try{
  58. ret = oldValue.apply(this, arguments);
  59. }finally{
  60. newValue.call(this);
  61. }
  62. return ret;
  63. } : function(){ newValue.call(this); }
  64. });
  65. })();
  66. });