aop.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.oo.aop"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.oo.aop"] = true;
  8. dojo.provide("dojox.lang.oo.aop");
  9. dojo.require("dojox.lang.oo.Decorator");
  10. dojo.require("dojox.lang.oo.general");
  11. (function(){
  12. var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general, ooa = oo.aop,
  13. isF = dojo.isFunction;
  14. // five decorators implementing light-weight AOP weaving
  15. /*=====
  16. ooa.before = md(function(name, newValue, oldValue){
  17. // summary: creates a "before" advise, by calling new function
  18. // before the old one
  19. // dummy body
  20. });
  21. ooa.around = md(function(name, newValue, oldValue){
  22. // summary: creates an "around" advise,
  23. // the previous value is passed as a first argument and can be null,
  24. // arguments are passed as a second argument
  25. // dummy body
  26. });
  27. =====*/
  28. // reuse existing decorators
  29. ooa.before = oog.before;
  30. ooa.around = oog.wrap;
  31. ooa.afterReturning = md(function(name, newValue, oldValue){
  32. // summary: creates an "afterReturning" advise,
  33. // the returned value is passed as the only argument
  34. return isF(oldValue) ?
  35. function(){
  36. var ret = oldValue.apply(this, arguments);
  37. newValue.call(this, ret);
  38. return ret;
  39. } : function(){ newValue.call(this); };
  40. });
  41. ooa.afterThrowing = md(function(name, newValue, oldValue){
  42. // summary: creates an "afterThrowing" advise,
  43. // the exception is passed as the only argument
  44. return isF(oldValue) ?
  45. function(){
  46. var ret;
  47. try{
  48. ret = oldValue.apply(this, arguments);
  49. }catch(e){
  50. newValue.call(this, e);
  51. throw e;
  52. }
  53. return ret;
  54. } : oldValue;
  55. });
  56. ooa.after = md(function(name, newValue, oldValue){
  57. // summary: creates an "after" advise,
  58. // it takes no arguments
  59. return isF(oldValue) ?
  60. function(){
  61. var ret;
  62. try{
  63. ret = oldValue.apply(this, arguments);
  64. }finally{
  65. newValue.call(this);
  66. }
  67. return ret;
  68. } : function(){ newValue.call(this); }
  69. });
  70. })();
  71. }