1 | define(["./aspect", "./on"], function(aspect, on){ |
---|
2 | // module: |
---|
3 | // dojo/Evented |
---|
4 | |
---|
5 | "use strict"; |
---|
6 | var after = aspect.after; |
---|
7 | function Evented(){ |
---|
8 | // summary: |
---|
9 | // A class that can be used as a mixin or base class, |
---|
10 | // to add on() and emit() methods to a class |
---|
11 | // for listening for events and emitting events: |
---|
12 | // example: |
---|
13 | // | define(["dojo/Evented", "dojo/_base/declare", "dojo/Stateful" |
---|
14 | // | ], function(Evented, declare, Stateful){ |
---|
15 | // | var EventedStateful = declare([Evented, Stateful], {...}); |
---|
16 | // | var instance = new EventedStateful(); |
---|
17 | // | instance.on("open", function(event){ |
---|
18 | // | ... do something with event |
---|
19 | // | }); |
---|
20 | // | |
---|
21 | // | instance.emit("open", {name:"some event", ...}); |
---|
22 | } |
---|
23 | Evented.prototype = { |
---|
24 | on: function(type, listener){ |
---|
25 | return on.parse(this, type, listener, function(target, type){ |
---|
26 | return after(target, 'on' + type, listener, true); |
---|
27 | }); |
---|
28 | }, |
---|
29 | emit: function(type, event){ |
---|
30 | var args = [this]; |
---|
31 | args.push.apply(args, arguments); |
---|
32 | return on.emit.apply(on, args); |
---|
33 | } |
---|
34 | }; |
---|
35 | return Evented; |
---|
36 | }); |
---|