-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
74 lines (73 loc) · 2.72 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
<!doctype html>
<html>
<head>
<title>Test Simple Hash Router</title>
<style>
#wrapper {
padding: 10px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
li a {
padding: 5px 10px;
}
</style>
</head>
<body>
<h1>Examples:</h1>
<p>Simple Hash Router + Cache Manager</p>
<ul>
<li><a href="#/">Home</a></li>
<li><a href="#/hello">Hello</a></li>
<li><a href="#/user/my-id">User</a></li>
<li><a href="#/your-ip">Your IP</a></li>
<li><a href="#/404">404</a></li>
</ul>
<div id="wrapper" style="margin: 5px;"></div>
<script src="hash-router.js"></script>
<script src="cache-manager.js"></script>
<script>
var cacheManager = new CacheManager;
var routes = {
'/': function() {
console.log('callback for "/"');
document.getElementById('wrapper').textContent = 'DEFAULT-HOME!';
},
'/hello': function() {
console.log('callback for /hello');
document.getElementById('wrapper').textContent = 'HELLO!';
},
'/user/:id': function(params, router) {
console.log('Route with params. Callback for /user/:id', params, params.id, r);
},
'/your-ip': function() {
console.log('callback for /your-ip');
var data = cacheManager.get('your-ip');
if (!data) {
fetch('https://api.ipify.org?format=json')
.then(function(response) { return response.json(); })
.then(function(json) {
cacheManager.set('your-ip', json);
document.getElementById('wrapper').textContent = 'IP: ' + json.ip;
});
} else {
console.log('serving from cache');
document.getElementById('wrapper').textContent = 'IP: ' + data.ip + ' (DATA FROM CACHE MANAGER)';
}
},
'*': function(params, router) {
console.log('Match all routes. Example: 404');
// Redirect to '/' all routes.
router.navigate('/');
}
};
var router = new HashRouter(routes);
</script>
</body>
</html>