-
Notifications
You must be signed in to change notification settings - Fork 14
/
Evented.js
40 lines (38 loc) · 1.25 KB
/
Evented.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/** @module decor/Evented */
define(["dcl/dcl", "dcl/advise"], function (dcl, advise) {
/**
* Base class to add `on()` and `emit()` methods to a class for listening for events and emitting events.
* @example
* var EventedSubClass = dcl(Evented, {...});
* var instance = new EventedSubClass();
* instance.on("open", function (event) {
* ... do something with event
* });
* instance.emit("open", {name: "some event", ...});
* @mixin module:decor/Evented
*/
return dcl(/** @lends module:decor/Evented# */ {
declaredClass: "decor/Evented",
/**
* Setup listener to be called when specified event is fired.
* @param {string} type - Name of event.
* @param {Function} listener - Callback for when event occurs.
* @returns {Object} Handle with `destroy()` method to stop listening to event.
*/
on: function (type, listener) {
return advise.before(this, "on" + type, listener);
},
/**
* Emit specified event.
* @param {string} type - Name of event.
* @param {...anything} var_args Parameters to pass to the listeners for this event.
*/
emit: function (type) {
var func = "on" + type;
if (this[func]) {
var args = Array.prototype.slice.call(arguments, 1);
this[func].apply(this, args);
}
}
});
});