general.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.general"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.oo.general"] = true;
  8. dojo.provide("dojox.lang.oo.general");
  9. dojo.require("dojox.lang.oo.Decorator");
  10. (function(){
  11. var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general,
  12. isF = dojo.isFunction;
  13. // generally useful decorators
  14. oog.augment = md(function(name, newValue, oldValue){
  15. // summary: add property, if it was not defined before
  16. return typeof oldValue == "undefined" ? newValue : oldValue;
  17. });
  18. oog.override = md(function(name, newValue, oldValue){
  19. // summary: override property only if it was already present
  20. return typeof oldValue != "undefined" ? newValue : oldValue;
  21. });
  22. oog.shuffle = md(function(name, newValue, oldValue){
  23. // summary: replaces arguments for an old method
  24. return isF(oldValue) ?
  25. function(){
  26. return oldValue.apply(this, newValue.apply(this, arguments));
  27. } : oldValue;
  28. });
  29. oog.wrap = md(function(name, newValue, oldValue){
  30. // summary: wraps the old values with a supplied function
  31. return function(){ return newValue.call(this, oldValue, arguments); };
  32. });
  33. oog.tap = md(function(name, newValue, oldValue){
  34. // summary: always returns "this" ignoring the actual return
  35. return function(){ newValue.apply(this, arguments); return this; };
  36. });
  37. oog.before = md(function(name, newValue, oldValue){
  38. // summary:
  39. // creates a chain of calls where the new method is called
  40. // before the old method
  41. return isF(oldValue) ?
  42. function(){
  43. newValue.apply(this, arguments);
  44. return oldValue.apply(this, arguments);
  45. } : newValue;
  46. });
  47. oog.after = md(function(name, newValue, oldValue){
  48. // summary:
  49. // creates a chain of calls where the new method is called
  50. // after the old method
  51. return isF(oldValue) ?
  52. function(){
  53. oldValue.apply(this, arguments);
  54. return newValue.apply(this, arguments);
  55. } : newValue;
  56. });
  57. })();
  58. }