forked from graniteds/jsonr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
103 lines (84 loc) · 3.42 KB
/
index.html
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
<!DOCTYPE html>
<!--
index.html
2013-07-23
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Author: Franck WOLFF (http://www.graniteds.org)
A sample HTML file using jsonr.
-->
<head>
<meta charset="utf-8" />
<title>JsonR Test</title>
<script type="text/javascript" src="jsonr.js"></script>
<script type="text/javascript">
function getAllCities() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 0)) {
var res = xhr.responseText,
cities = JSONR.parse(res),
div = document.getElementById('data');
var innerHTML = '<h3>Cities</h3><div>';
var country = null;
for (var i = 0; i < cities.length; i++) {
var city = cities[i];
if (country !== null && city.country !== country) {
innerHTML += '<br />';
}
country = city.country;
innerHTML += '<div>' +
city.name + " - " +
city.country.name + ' - ' +
city.country.language +
(city === city.country.capital ? ' (Capital)' : '') +
'</div>';
}
innerHTML += '</div>';
innerHTML +=
'<div><h3>JsonR data with revealed references (eg. "@1" -> @1{...}):</h3></div>' +
'<pre>' + JSONR.revealReferences(res) + '</pre>';
div.innerHTML = innerHTML;
}
};
xhr.open('POST', 'cities', true);
xhr.send();
}
function createBrazilianCities() {
var brazil = { name: 'Brazil', language: 'Portuguese' },
newCities = [
{ name: 'Brasilia', country: brazil },
{ name: 'Rio de Janeiro', country: brazil },
{ name: 'Bel Horizonte', country: brazil },
];
brazil.capital = newCities[0];
var xhr = new XMLHttpRequest();
xhr.open('POST', 'new-cities', false);
xhr.send(JSONR.stringify(newCities, null, '\t'));
getAllCities();
}
function creatChineseCities() {
var china = { name: 'China', language: 'Chinese' },
newCities = [
{ name: 'Beijing', country: china },
{ name: 'Shanghai', country: china },
{ name: 'Hong Kong', country: china },
];
china.capital = newCities[0];
var xhr = new XMLHttpRequest();
xhr.open('POST', 'new-cities', false);
xhr.send(JSONR.stringify(newCities, null, '\t'));
getAllCities();
}
</script>
</head>
<body>
<h2>JsonR Test</h2>
<input type="button" value="getAllCities()" onclick="getAllCities()" />
<input type="button" value="createBrazilianCities()" onclick="createBrazilianCities()" />
<input type="button" value="creatChineseCities()" onclick="creatChineseCities()" />
<br />
<div id="data">
</div>
</body>
</html>