Skip to content

Instantly share code, notes, and snippets.

@zedar
Created January 8, 2014 23:46
Show Gist options
  • Save zedar/8326924 to your computer and use it in GitHub Desktop.
Save zedar/8326924 to your computer and use it in GitHub Desktop.
Destroyable dojo class - keep track of reference (handlers) to DOM events and event subscription. Remove them (automatically) when class instance is destoyed. dijit/Destroyable is a mixin class to track handles and release them.
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/topic",
"dijit/Destroyable"
], function(declare, lang, topic, Destroyable) {
var _class = declare("DestroyableController", [Destroyable], {
constructor: function(args) {
declare.safeMixin();
// Use own function from Destroyable interface to keep track of handlers
this.own (
topic.subscribe("/my/event/1", lang.hitch(this, this._onEventOne)),
topic.subscribe("/my/event/2", lang.hitch(this, this._onEventTwo))
);
},
_onEventOne: function(eventData) {
// Overview:
// Handler for /my/event/1
// Args:
// eventData - data from topic.publish("/my/event/1", eventData)
},
_onEventTwo: function(eventData) {
// Overview:
// Handler for /my/event/2
// Args:
// eventData - data from topic.publish("/my/event/2", eventData)
}
});
return _class;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment