Destroyable.js.uncompressed.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. define("dijit/Destroyable", [
  2. "dojo/_base/array", // array.forEach array.map
  3. "dojo/aspect",
  4. "dojo/_base/declare"
  5. ], function(array, aspect, declare){
  6. // module:
  7. // dijit/Destroyable
  8. return declare("dijit.Destroyable", null, {
  9. // summary:
  10. // Mixin to track handles and release them when instance is destroyed.
  11. // description:
  12. // Call this.own(...) on list of handles (returned from dojo/aspect, dojo/on,
  13. // dojo/Stateful::watch, or any class (including widgets) with a destroyRecursive() or destroy() method.
  14. // Then call destroy() later to destroy this instance and release the resources.
  15. destroy: function(/*Boolean*/ preserveDom){
  16. // summary:
  17. // Destroy this class, releasing any resources registered via own().
  18. this._destroyed = true;
  19. },
  20. own: function(){
  21. // summary:
  22. // Track specified handles and remove/destroy them when this instance is destroyed, unless they were
  23. // already removed/destroyed manually.
  24. // tags:
  25. // protected
  26. // returns:
  27. // The array of specified handles, so you can do for example:
  28. // | var handle = this.own(on(...))[0];
  29. array.forEach(arguments, function(handle){
  30. var destroyMethodName =
  31. "destroyRecursive" in handle ? "destroyRecursive" : // remove "destroyRecursive" for 2.0
  32. "destroy" in handle ? "destroy" :
  33. "remove";
  34. // When this.destroy() is called, destroy handle. Since I'm using aspect.before(),
  35. // the handle will be destroyed before a subclass's destroy() method starts running, before it calls
  36. // this.inherited() or even if it doesn't call this.inherited() at all. If that's an issue, make an
  37. // onDestroy() method and connect to that instead.
  38. var odh = aspect.before(this, "destroy", function(preserveDom){
  39. handle[destroyMethodName](preserveDom);
  40. });
  41. // If handle is destroyed manually before this.destroy() is called, remove the listener set directly above.
  42. var hdh = aspect.after(handle, destroyMethodName, function(){
  43. odh.remove();
  44. hdh.remove();
  45. }, true);
  46. }, this);
  47. return arguments; // handle
  48. }
  49. });
  50. });