NodeList-data.html
4.59 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Testing dojo._data / NodeList.data</title>
<script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug:true"></script>
<script type="text/javascript">
require(["doh", "dojo/dom-construct", "dojo/query", "dojo/NodeList-data", "dojo/domReady!"],
function(doh, domConstruct, $, NodeList){
var len = function(obj){
var x = 0;
for(var i in obj){ x++ }
return x;
};
doh.register([
function sanity(t){
var list = $("#foo");
var lis = $("#bar > li");
t.is(1, list.length, "i'm not insane");
t.is(2, lis.length, "li's are sane, too");
list.data("a", "b");
lis.data("a", "b");
t.is("b", list.data("a")[0]);
t.is(["b"], list.data("a"));
t.is(["b","b"], lis.data("a"));
t.is("b", lis.data("a")[0]);
},
function basicdata(t){
var list = $('#foo');
list.data("bar", 6)
.data("baz", "a")
.data("bam", [1,2,3])
.data("foo", { a:"b" })
;
var newlist = $("#foo");
t.is(6, newlist.data("bar")[0]);
t.is("a", newlist.data("baz")[0]);
t.is(3, newlist.data("bam")[0].length);
t.is(1, newlist.data("bam")[0][0]);
t.is("b", newlist.data("foo")[0].a);
},
function hashdata(t){
$("#foo").data({
bar:"baz",
foo:"bap"
});
t.is("baz", $('#foo').data("bar")[0]);
t.is("bap", $('#foo').data("foo")[0]);
},
function butdoesitchain(t){
$("#foo").data("bar", 42).style("color", "red");
t.is(42, $("#foo").data("bar")[0]);
},
function getanobjectback(t){
$("#foo").data("afoo", 1);
$("#foo").data("bfoo", 2);
var obj = $("#foo").data()[0];
t.is(1, obj.afoo);
t.is(2, obj.bfoo);
},
function plaindata(t){
$("#bar li").data("bar", 42)
.forEach(function(n){
t.is(42, dojo._nodeData(n, "bar"));
});
},
function removeData(t){
$("#bar li").removeData("bar");
$("#bar li").forEach(function(n){
t.t(!dojo._nodeData(n, "bar"));
});
$("#bar li").data({
a:"b", c:"d", e:"f"
});
$("#bar li").removeData();
var data = $("#bar li").data()[0];
t.f(data.a);
t.f(data.c);
t.f(data.e);
},
function multidata(t){
var ret = $("#bar li");
t.is(2, ret.length, "sanity: 2 (0..1) li's in query");
ret = ret.data("bar", "baz").data();
t.is(ret[0].bar, "baz", "item 0 was set");
t.is(ret[1].bar, "baz", "item 1 was set");
$("li").at(0).removeData();
var ret2 = $("#bar li").data();
t.is(ret.length, 2, "sanity: 2 (0..1) li's in query");
t.f(ret2[0].bar, "at(0) was removed");
t.is(ret2[1].bar, "baz", "at(1) was untouched");
},
function obj(t){
var x = $("#foo").data({ bar: { baz:"bam" }}).data("bar");
t.is("bam", x[0].baz);
},
function cleanData(t){
if(!NodeList._nodeDataCache){
doh.debug("We must be testing a built Dojo. No access to dataCache");
return;
}
var me = $("#bar li").data("die", "yes");
me.at(0).attr("id", "killme");
var data = me.data();
t.is(2, me.length);
t.is("yes", me.data("die")[0]);
t.is("yes", me.data("die")[1]);
var l = len(NodeList._nodeDataCache);
domConstruct.destroy("killme");
NodeList._gcNodeData();
t.is(l - 1, len(NodeList._nodeDataCache), "one item removed because destroyed");
me = $("#bar li");
t.is(1, me.length);
},
function literals(t){
// this is an implementation detail. object literals count,
// but doesn't mean you should use them.
var x = { a:1 };
var one = $("#lit span").data("literal", x);
x.a++;
t.is(2, $("#lit span").data("literal")[0].a);
t.is(2, $("#lit span").data("literal")[1].a);
},
function clearall(t){
if(!NodeList._nodeDataCache){
doh.debug("We must be testing a built Dojo. No access to dataCache");
return;
}
var l = len(NodeList._nodeDataCache);
t.t(l, "there is stuff in the cache");
$("#b > *").forEach(domConstruct.destroy);
dojo._gcNodeData();
t.is(0, len(NodeList._nodeDataCache), "no longer stuff in the cache");
}
]);
doh.run();
});
</script>
</head>
<body>
<div id="b">
<div id="foo">woot.</div>
<ul id="bar">
<li>baz</li>
<li>bam</li>
</ul>
<p id="lit"><span>hi</span><span>there</span></p>
</div>
</body>
</html>