-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvented.js
More file actions
54 lines (47 loc) · 1.45 KB
/
Evented.js
File metadata and controls
54 lines (47 loc) · 1.45 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(function(define){
'use strict';
define(function(require){
var on = require('./main');
var emit = require('./emit');
var meld = require('meld');
function noop(){}
/**
* A class that can be used as a mixin or base class,
* to add on() and emit() methods to a class
* for listening for events and emitting events:
*
* @example
* define(["dojo/Evented"], function(Evented){
* var EventedWidget = dojo.declare([Evented, dijit._Widget], {});
* widget = new EventedWidget();
* widget.on("open", function(event){
* // do something with event
* })
* });
*
* widget.emit("open", {name:"some event"});
*/
function Evented(){}
Evented.prototype = {
on: function(type, listener){
return on.parse(this, type, listener, function(target, type){
// meld requires the method to exist on the object
var method = 'on' + type;
if(!(method in target)){
target[method] = noop;
}
return meld.on(target, 'on' + type, listener);
});
},
emit: function(type, event){
var args = [this];
args.push.apply(args, arguments);
return emit.apply(on, args);
}
};
return Evented;
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));