/
fall-through-cache.html
170 lines (142 loc) · 3.94 KB
/
fall-through-cache.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
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
<body>
<a href="#complete">Completed</a>
<a href="#incomplete">Incomplete</a>
<div id='list'></div>
<script src="../../node_modules/steal/steal.js" id="demo-source">
import connect from "can-connect";
import "can-connect/data/url/";
import "can-connect/constructor/";
import "can-connect/constructor/store/";
import "can-connect/data/localstorage-cache/";
import "can-connect/fall-through-cache/";
import fixture from "can-fixture";
import $ from "jquery";
import helpers from "can-connect/helpers/";
var cache = connect(['data-localstorage-cache'],{
name: "todos"
});
// A connection that gets todos data
var todosConnection = connect([
"fall-through-cache",
"constructor",
"constructor-store",
"data-url"],{
url: "/todos",
cacheConnection: cache
});
var todoItem = function(todo) {
var li = $("<li>");
var update = function(){
if(todo.complete) {
li.css("text-decoration","line-through");
} else {
li.css("text-decoration","");
}
li.text(todo.id+": "+todo.name);
};
update();
Object.observe(todo, update, ["add", "update", "delete"] );
todosConnection.addInstanceReference(todo);
li.bind("removed", function(){
Object.unobserve(todo, update, ["add", "update", "delete"] );
todosConnection.deleteInstanceReference(todo);
});
return li;
};
var todoList = function(set){
var element = $('<ul>Loading</ul>');
var todos;
var update = function(changes){
helpers.forEach.call(changes, function(change){
if(change.type === "add") {
// Create the `todoItem` and insert it in the right spot
var li = todoItem(todos[change.name]).hide();
if(element.children().length) {
element.children().eq(change.name-1).after( li );
} else {
element.append(li);
}
li.fadeIn();
}
else if(change.type === "delete") {
// Find the `todoItem` to remove.
// Trigger an event so it can tear itself down.
// Animate and remove the `todoItem` element.
var li = element.children().eq( change.name );
li.triggerHandler("remove");
li.fadeOut(function(){
li.remove();
});
}
else if(change.type === "update" && !isNaN(+change.name)) {
// replace the old item with a new one
var li = element.children().eq( change.name );
li.triggerHandler("remove");
var newLi = todoItem(todos[change.name]).hide();
li.replaceWith(newLi);
newLi.fadeIn();
}
});
};
todosConnection.getList(set).then(function(retrievedTodos){
element.empty();
todos = retrievedTodos;
todosConnection.addListReference(todos);
Object.observe(todos, update, ["add", "update", "delete"] );
update(todos.map(function(todo, i){
return {
type: "add",
name: ""+i
};
}));
});
element.bind("removed", function(){
update(todos.reverse().map(function(todo, i){
return {
type: "remove",
name: ""+i
};
}));
Object.unobserve(todos, update, ["add", "update", "delete"] );
todosConnection.deleteListReference(todos);
});
return element;
};
// When the hash changes, update which `todoList` is displayed.
var updatePage = function(){
var complete = window.location.hash !== "#incomplete";
$("#list").children().remove().triggerHandler("remove");
var ul = todoList({complete: complete});
$("#list").html(ul);
};
$(window).bind("hashchange", updatePage);
// Trap ajax requests to return a random list of todos
fixture.delay = 1000;
fixture({
"GET /todos": function(request){
var todos = randomTodos(request.data.complete == "true");
return {data: todos};
}
});
function randomTodos(complete){
var verbs = ["do","wash","mow","clean","take out"];
var names = ["dishes","lawn","garbage", "laundry","windows","floor"];
var ids = [];
for(var i =0; i < 10; i++) {
ids.push(i);
}
var todos = [];
var total = fixture.rand(7) + 1;
for(var i = 0; i < total; i++){
todos.push({
id: fixture.rand(ids,1)[0],
name: fixture.rand(verbs,1)[0]+" "+fixture.rand(names,1)[0],
complete: complete
});
}
return todos;
};
// Kick off current page
updatePage();
</script>
</body>