Skip to content

Commit 9990460

Browse files
committed
Added optional scope to all event bindings
1 parent 0b1e48a commit 9990460

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

v3/src/events/EventBinding.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ var EventBinding = new Class({
5858
return (this.get(callback));
5959
},
6060

61-
add: function (callback, priority, once)
61+
// Called by EventDispatcher.on and EventDispatcher.once
62+
add: function (callback, priority, scope, once)
6263
{
64+
// EventListener
6365
var listener = this.get(callback);
6466

6567
if (!listener)
6668
{
6769
// The listener doesn't exist, so create one
68-
listener = EventListener(this.type, callback, priority, once);
70+
listener = EventListener(this.type, callback, scope, priority, once);
6971
}
7072
else
7173
{
@@ -148,14 +150,16 @@ var EventBinding = new Class({
148150

149151
for (var i = 0; i < this.active.length; i++)
150152
{
153+
// EventListener
151154
listener = this.active[i];
152155

153156
if (listener.state !== CONST.LISTENER_ACTIVE)
154157
{
155158
continue;
156159
}
157160

158-
listener.callback.call(this.dispatcher, event);
161+
// listener.callback.call(this.dispatcher, event);
162+
listener.callback.call(listener.scope, event);
159163

160164
// Has the callback changed the state of this binding?
161165
if (this.state !== CONST.DISPATCHER_DISPATCHING)

v3/src/events/EventDispatcher.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,31 @@ var EventDispatcher = new Class({
3131
return this.bindings[type];
3232
},
3333

34-
on: function (type, listener, priority)
34+
on: function (type, listener, priority, scope)
3535
{
3636
if (priority === undefined) { priority = 0; }
37+
if (scope === undefined) { scope = this; }
3738

3839
var binding = this.createBinding(type);
3940

4041
if (binding)
4142
{
42-
binding.add(listener, priority, false);
43+
binding.add(listener, priority, scope, false);
4344
}
4445

4546
return this;
4647
},
4748

48-
once: function (type, listener, priority)
49+
once: function (type, listener, priority, scope)
4950
{
5051
if (priority === undefined) { priority = 0; }
52+
if (scope === undefined) { scope = this; }
5153

5254
var binding = this.createBinding(type);
5355

5456
if (binding)
5557
{
56-
binding.add(listener, priority, true);
58+
binding.add(listener, priority, scope, true);
5759
}
5860

5961
return this;

v3/src/events/EventListener.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var CONST = require('./const');
22

3-
var EventListener = function (type, callback, priority, once)
3+
var EventListener = function (type, callback, scope, priority, once)
44
{
55
return {
66
type: type,
77
callback: callback,
8+
scope: scope,
89
priority: priority,
910
once: once,
1011
state: CONST.LISTENER_PENDING

0 commit comments

Comments
 (0)