diff options
author | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
---|---|---|
committer | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
commit | 7c97d17aaf373339a8bcd917ad59ca6018148f0d (patch) | |
tree | 5a3c04f0f9529be392c1263d3feb75806eb43797 /lib/dijit/Destroyable.js.uncompressed.js | |
parent | 70db7424e7068701e60cc5bcdfe8f858be508179 (diff) | |
parent | c670a80ddd9b03bd4ea6d940a9ed682fd26248d7 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'lib/dijit/Destroyable.js.uncompressed.js')
-rw-r--r-- | lib/dijit/Destroyable.js.uncompressed.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/dijit/Destroyable.js.uncompressed.js b/lib/dijit/Destroyable.js.uncompressed.js new file mode 100644 index 000000000..ee6b8893e --- /dev/null +++ b/lib/dijit/Destroyable.js.uncompressed.js @@ -0,0 +1,59 @@ +define("dijit/Destroyable", [ + "dojo/_base/array", // array.forEach array.map + "dojo/aspect", + "dojo/_base/declare" +], function(array, aspect, declare){ + +// module: +// dijit/Destroyable + +return declare("dijit.Destroyable", null, { + // summary: + // Mixin to track handles and release them when instance is destroyed. + // description: + // Call this.own(...) on list of handles (returned from dojo/aspect, dojo/on, + // dojo/Stateful::watch, or any class (including widgets) with a destroyRecursive() or destroy() method. + // Then call destroy() later to destroy this instance and release the resources. + + destroy: function(/*Boolean*/ preserveDom){ + // summary: + // Destroy this class, releasing any resources registered via own(). + this._destroyed = true; + }, + + own: function(){ + // summary: + // Track specified handles and remove/destroy them when this instance is destroyed, unless they were + // already removed/destroyed manually. + // tags: + // protected + // returns: + // The array of specified handles, so you can do for example: + // | var handle = this.own(on(...))[0]; + + array.forEach(arguments, function(handle){ + var destroyMethodName = + "destroyRecursive" in handle ? "destroyRecursive" : // remove "destroyRecursive" for 2.0 + "destroy" in handle ? "destroy" : + "remove"; + + // When this.destroy() is called, destroy handle. Since I'm using aspect.before(), + // the handle will be destroyed before a subclass's destroy() method starts running, before it calls + // this.inherited() or even if it doesn't call this.inherited() at all. If that's an issue, make an + // onDestroy() method and connect to that instead. + var odh = aspect.before(this, "destroy", function(preserveDom){ + handle[destroyMethodName](preserveDom); + }); + + // If handle is destroyed manually before this.destroy() is called, remove the listener set directly above. + var hdh = aspect.after(handle, destroyMethodName, function(){ + odh.remove(); + hdh.remove(); + }, true); + }, this); + + return arguments; // handle + } +}); + +}); |