Observable-more_wev8.js
6.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*!
* Ext JS Library 3.0.0
* Copyright(c) 2006-2009 Ext JS, LLC
* licensing@extjs.com
* http://www.extjs.com/license
*/
/**
* @class Ext.util.Observable
*/
Ext.apply(Ext.util.Observable.prototype, function(){
// this is considered experimental (along with beforeMethod, afterMethod, removeMethodListener?)
// allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
// private
function getMethodEvent(method){
var e = (this.methodEvents = this.methodEvents ||
{})[method], returnValue, v, cancel, obj = this;
if (!e) {
this.methodEvents[method] = e = {};
e.originalFn = this[method];
e.methodName = method;
e.before = [];
e.after = [];
var makeCall = function(fn, scope, args){
if (!Ext.isEmpty(v = fn.apply(scope || obj, args))) {
if (Ext.isObject(v)) {
returnValue = !Ext.isEmpty(v.returnValue) ? v.returnValue : v;
cancel = !!v.cancel;
}
else
if (v === false) {
cancel = true;
}
else {
returnValue = v;
}
}
};
this[method] = function(){
var args = Ext.toArray(arguments);
returnValue = v = undefined;
cancel = false;
Ext.each(e.before, function(b){
makeCall(b.fn, b.scope, args);
if (cancel) {
return returnValue;
}
});
if (!Ext.isEmpty(v = e.originalFn.apply(obj, args))) {
returnValue = v;
}
Ext.each(e.after, function(a){
makeCall(a.fn, a.scope, args);
if (cancel) {
return returnValue;
}
});
return returnValue;
};
}
return e;
}
return {
// these are considered experimental
// allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
// adds an "interceptor" called before the original method
beforeMethod: function(method, fn, scope){
getMethodEvent.call(this, method).before.push({
fn: fn,
scope: scope
});
},
// adds a "sequence" called after the original method
afterMethod: function(method, fn, scope){
getMethodEvent.call(this, method).after.push({
fn: fn,
scope: scope
});
},
removeMethodListener: function(method, fn, scope){
var e = getMethodEvent.call(this, method), found = false;
Ext.each(e.before, function(b, i, arr){
if (b.fn == fn && b.scope == scope) {
arr.splice(i, 1);
found = true;
return false;
}
});
if (!found) {
Ext.each(e.after, function(a, i, arr){
if (a.fn == fn && a.scope == scope) {
arr.splice(i, 1);
return false;
}
});
}
},
/**
* Relays selected events from the specified Observable as if the events were fired by <tt><b>this</b></tt>.
* @param {Object} o The Observable whose events this object is to relay.
* @param {Array} events Array of event names to relay.
*/
relayEvents: function(o, events){
var me = this;
function createHandler(ename){
return function(){
return me.fireEvent.apply(me, [ename].concat(Ext.toArray(arguments)));
};
}
Ext.each(events, function(ename){
me.events[ename] = me.events[ename] || true;
o.on(ename, createHandler(ename), me);
});
},
/**
* Used to enable bubbling of events
* @param {Object} events
*/
enableBubble: function(events){
var me = this;
events = Ext.isArray(events) ? events : Ext.toArray(arguments);
Ext.each(events, function(ename){
ename = ename.toLowerCase();
var ce = me.events[ename] || true;
if (typeof ce == "boolean") {
ce = new Ext.util.Event(me, ename);
me.events[ename] = ce;
}
ce.bubble = true;
});
}
};
}());
/**
* Starts capture on the specified Observable. All events will be passed
* to the supplied function with the event name + standard signature of the event
* <b>before</b> the event is fired. If the supplied function returns false,
* the event will not fire.
* @param {Observable} o The Observable to capture
* @param {Function} fn The function to call
* @param {Object} scope (optional) The scope (this object) for the fn
* @static
*/
Ext.util.Observable.capture = function(o, fn, scope){
o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
};
/**
* Sets observability on the passed class constructor.<p>
* <p>This makes any event fired on any instance of the passed class also fire a single event through
* the <i>class</i> allowing for central handling of events on many instances at once.</p>
* <p>Usage:</p><pre><code>
Ext.util.Observable.observeClass(Ext.data.Connection);
Ext.data.Connection.on('beforerequest', function(con, options) {
console.log("Ajax request made to " + options.url);
});</code></pre>
* @param {Function} c The class constructor to make observable.
* @static
*/
Ext.util.Observable.observeClass = function(c){
Ext.apply(c, new Ext.util.Observable());
c.prototype.fireEvent = function(){
return (c.fireEvent.apply(c, arguments) !== false) &&
(Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false);
};
};