source: Dev/trunk/src/client/dojo/tests/NodeList-data.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.6 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2        "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4        <head>
5                <title>Testing dojo._data / NodeList.data</title>
6                <script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug:true"></script>
7                <script type="text/javascript">
8                        require(["doh", "dojo/dom-construct", "dojo/query", "dojo/NodeList-data", "dojo/domReady!"],
9                                        function(doh, domConstruct, $, NodeList){
10                                var len = function(obj){
11                                        var x = 0;
12                                        for(var i in obj){ x++ }
13                                        return x;
14                                };
15
16                                doh.register([
17                                        function sanity(t){
18
19                                                var list = $("#foo");
20                                                var lis = $("#bar > li");
21
22                                                t.is(1, list.length, "i'm not insane");
23                                                t.is(2, lis.length, "li's are sane, too");
24
25                                                list.data("a", "b");
26                                                lis.data("a", "b");
27
28                                                t.is("b", list.data("a")[0]);
29                                                t.is(["b"], list.data("a"));
30
31                                                t.is(["b","b"], lis.data("a"));
32                                                t.is("b", lis.data("a")[0]);
33                                        },
34
35                                        function basicdata(t){
36
37                                                var list = $('#foo');
38
39                                                list.data("bar", 6)
40                                                        .data("baz", "a")
41                                                        .data("bam", [1,2,3])
42                                                        .data("foo", { a:"b" })
43                                                ;
44
45                                                var newlist = $("#foo");
46
47                                                t.is(6, newlist.data("bar")[0]);
48                                                t.is("a", newlist.data("baz")[0]);
49                                                t.is(3, newlist.data("bam")[0].length);
50                                                t.is(1, newlist.data("bam")[0][0]);
51                                                t.is("b", newlist.data("foo")[0].a);
52
53                                        },
54
55                                        function hashdata(t){
56
57                                                $("#foo").data({
58                                                        bar:"baz",
59                                                        foo:"bap"
60                                                });
61
62                                                t.is("baz", $('#foo').data("bar")[0]);
63                                                t.is("bap", $('#foo').data("foo")[0]);
64
65                                        },
66
67                                        function butdoesitchain(t){
68
69                                                $("#foo").data("bar", 42).style("color", "red");
70                                                t.is(42, $("#foo").data("bar")[0]);
71                                        },
72
73                                        function getanobjectback(t){
74
75                                                $("#foo").data("afoo", 1);
76                                                $("#foo").data("bfoo", 2);
77
78                                                var obj = $("#foo").data()[0];
79
80                                                t.is(1, obj.afoo);
81                                                t.is(2, obj.bfoo);
82
83                                        },
84
85                                        function plaindata(t){
86                                                $("#bar li").data("bar", 42)
87                                                        .forEach(function(n){
88                                                                t.is(42, dojo._nodeData(n, "bar"));
89                                                        });
90                                        },
91
92                                        function removeData(t){
93                                                $("#bar li").removeData("bar");
94                                                $("#bar li").forEach(function(n){
95                                                        t.t(!dojo._nodeData(n, "bar"));
96                                                });
97
98                                                $("#bar li").data({
99                                                        a:"b", c:"d", e:"f"
100                                                });
101
102                                                $("#bar li").removeData();
103                                                var data = $("#bar li").data()[0];
104
105                                                t.f(data.a);
106                                                t.f(data.c);
107                                                t.f(data.e);
108                                        },
109
110                                        function multidata(t){
111
112                                                var ret = $("#bar li");
113                                                t.is(2, ret.length, "sanity: 2 (0..1) li's in query");
114                                                ret = ret.data("bar", "baz").data();
115
116                                                t.is(ret[0].bar, "baz", "item 0 was set");
117                                                t.is(ret[1].bar, "baz", "item 1 was set");
118
119                                                $("li").at(0).removeData();
120
121                                                var ret2 = $("#bar li").data();
122                                                t.is(ret.length, 2, "sanity: 2 (0..1) li's in query");
123                                                t.f(ret2[0].bar, "at(0) was removed");
124                                                t.is(ret2[1].bar, "baz", "at(1) was untouched");
125
126                                        },
127
128                                        function obj(t){
129                                                var x = $("#foo").data({ bar: { baz:"bam" }}).data("bar");
130                                                t.is("bam", x[0].baz);
131
132                                        },
133
134                                        function cleanData(t){
135
136                                                if(!NodeList._nodeDataCache){
137                                                        doh.debug("We must be testing a built Dojo. No access to dataCache");
138                                                        return;
139                                                }
140
141                                                var me = $("#bar li").data("die", "yes");
142                                                me.at(0).attr("id", "killme");
143                                                var data = me.data();
144
145                                                t.is(2, me.length);
146                                                t.is("yes", me.data("die")[0]);
147                                                t.is("yes", me.data("die")[1]);
148
149                                                var l = len(NodeList._nodeDataCache);
150
151                                                domConstruct.destroy("killme");
152                                                NodeList._gcNodeData();
153
154                                                t.is(l - 1, len(NodeList._nodeDataCache), "one item removed because destroyed");
155                                                me = $("#bar li");
156                                                t.is(1, me.length);
157
158                                        },
159
160                                        function literals(t){
161                                                // this is an implementation detail. object literals count,
162                                                // but doesn't mean you should use them.
163                                                var x = { a:1 };
164
165                                                var one = $("#lit span").data("literal", x);
166                                                x.a++;
167
168                                                t.is(2, $("#lit span").data("literal")[0].a);
169                                                t.is(2, $("#lit span").data("literal")[1].a);
170
171                                        },
172
173                                        function clearall(t){
174
175                                                if(!NodeList._nodeDataCache){
176                                                        doh.debug("We must be testing a built Dojo. No access to dataCache");
177                                                        return;
178                                                }
179
180                                                var l = len(NodeList._nodeDataCache);
181                                                t.t(l, "there is stuff in the cache");
182
183                                                $("#b > *").forEach(domConstruct.destroy);
184                                                dojo._gcNodeData();
185                                                t.is(0, len(NodeList._nodeDataCache), "no longer stuff in the cache");
186                                        }
187                                ]);
188
189                                doh.run();
190                        });
191                </script>
192        </head>
193        <body>
194                <div id="b">
195                        <div id="foo">woot.</div>
196                        <ul id="bar">
197                                <li>baz</li>
198                                <li>bam</li>
199                        </ul>
200                        <p id="lit"><span>hi</span><span>there</span></p>
201                </div>
202        </body>
203</html>
Note: See TracBrowser for help on using the repository browser.