-
Notifications
You must be signed in to change notification settings - Fork 5
/
connectlive.js
78 lines (66 loc) · 1.86 KB
/
connectlive.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
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
define(["dojo"], function(dojo){
var d = dojo, cache = {}, connected = {},
_liveHandler = function(e){
// summary: Handles all the delegated clicks.
var t = e && e.type;
// only look through the selectors for this event
if(t && cache[t]){
var n = e.target,
nl = d.query(n),
a = arguments
;
// go over each selector
for(var i in cache[t]){
// :( this makes me horribly sad:
if(nl.filter(i).length || (e.bubbles && d.query(i + " *").indexOf(n) >= 0)){
d.forEach(cache[t][i], function(fn){
fn.apply(n, a);
});
}
}
}
}
;
d.connectLive = function(selector, event, fn, method){
// summary: Delegates and event to nodes matching selector
//
// example:
// | dojo.connectLive("a.external", "onclick", function(e){
// | // if something clicked and is an anchor with class='external'
// | // this will execute;
// | })
// do the hitch syntax thing:
if(fn && method){
fn = d.hitch(fn, method);
}
// normalize the event
event = event && event.slice(0, 2) == "on" ? event.slice(2) : event
if(!cache[event]){
cache[event] = {};
}
// create an array to hold functions to call when this selector matches
if(!cache[event][selector]){
cache[event][selector] = [];
}
cache[event][selector].push(fn);
// connect a delegated
if(!connected[event]){
// fixme: some stuff needs to be on dojo.body()
connected[event] = d.connect(d.body(), event, _liveHandler);
}
return [selector, event, fn];
}
d.disconnectLive = function(handle){
var event = handle[1], selector = handle[0], fn = handle[2];
if(cache[event] && cache[event][selector]){
d.forEach(cache[event][selector], function(f, i, a){
if(fn == f){
a.splice(i, 1);
}
console.log(a.length);
});
}
console.log(event, cache[event], connected[event]);
}
return d;
});