/
index.html
253 lines (236 loc) · 8.76 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="Knockout-Apollo ToDo List Examples">
<title>Knockout-Apollo ToDo List Examples</title>
<link rel="stylesheet" href="demo/src/todo.css">
<script type="text/javascript" src="demo/bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="demo/bower_components/knockout/dist/knockout.js"></script>
<script type="text/javascript" src="demo/bower_components/ko.plus/src/ko.command.js"></script>
<script type="text/javascript" src="demo/bower_components/knockoutcrud/knockoutcrud.js"></script>
<script type="text/javascript" src="demo/src/TodoList.js"></script>
<script type="text/javascript" src="demo/src/components.js"></script>
<script type="text/javascript" src="src/knockout-apollo.js"></script>
<script type="text/javascript" src="dist/demo.js"></script>
</head>
<body>
<h1>Knockout ToDo List Examples integrated with Apollo Client</h1>
<h2>1) Directly edit the Observable Array</h2>
<p>Approach: CRUD the Observable Array. Update the server upon save.</p>
<div class="left-panel">
<!-- ko ifnot: todoList.beforeEdit -->
<button data-bind="click: prepareCollectionEdit" class="button">Edit Todo List</button>
<!-- /ko -->
<!-- ko if: todoList.beforeEdit -->
<button data-bind="click: save" class="button">Save</button>
<button data-bind="click: cancel" class="button">Cancel</button>
<!-- /ko -->
</div>
<!-- ko if: todoList.beforeEdit -->
<div class="content-panel">
<button data-bind="click: addTask" class="button">Add Task</button>
</div>
<div class="content-panel">
<div data-bind="foreach: todoList">
<div class="note" data-bind="click: $root.editItem, css: completed() ? 'complete' : 'incomplete'">
<div>
<span data-bind="click: $root.todoList.deleteItem, clickBubble: false" class="remove">X</span>
</div>
<div>
<!-- ko if: _id.isSelected -->
<input type="text" data-bind="textInput: task, hasFocus: _id.isSelected" class="todo-input">
<!-- /ko -->
<!-- ko ifnot: _id.isSelected -->
<p><span data-bind="text: task"></span></p>
<!-- /ko -->
</div>
<label>
<input type="checkbox" data-bind="checked: completed">Complete
</label>
</div>
</div>
</div>
<!-- /ko -->
<div class="content-panel">
<div data-bind="visible: todoList.apollo.isRunning">Communicating with Server...</div>
<table data-bind="visible: !todoList.apollo.isRunning()">
<thead>
<tr>
<th>index</th>
<th>_id</th>
<th>Task</th>
<th>Completed</th>
<th>isSelected</th>
</tr>
</thead>
<tbody data-bind="foreach: todoList">
<tr>
<td data-bind='text: $index'></td>
<td data-bind='text: _id'></td>
<td data-bind='text: task'></td>
<td data-bind='text: completed'></td>
<td data-bind='text: _id.isSelected'></td>
</tr>
</tbody>
</table>
<!-- ko if:todoList.messages().length -->
<div class="alert success">
<!-- ko foreach: todoList.messages -->
<stong data-bind="text: $data.title"></stong>
<pre data-bind="text: $data.content"></pre>
<!-- /ko -->
</div>
<!-- /ko -->
</div>
<hr/>
<h2>2) Adding a new object to an Observable Array with a cancellation option</h2>
<p>Subscribe to changes in the ViewModel and update the server for each change</p>
<!-- ko ifnot: todoList2.itemForEditing() || todoList2.itemForAdding() -->
<div class="content-panel">
<button data-bind="click: addTask2" class="button">Add Task</button>
</div>
<div class="content-panel">
<div data-bind="foreach: todoList2">
<div class="note" data-bind="click: $root.todoList2.selectItem, css: completed() ? 'complete' : 'incomplete'">
<div>
<span data-bind="click: $root.todoList2.deleteItem, clickBubble: false" class="remove">X</span>
</div>
<div data-bind="click: $root.todoList2.selectItem">
<p><span data-bind="text: task"></span></p>
<p data-bind="if: completed"><span>Completed</span></p>
</div>
</div>
</div>
</div>
<!-- /ko -->
<div class="content-panel" data-bind="with: todoList2.itemForEditing">
<h2>Editing</h2>
<input type="text" data-bind="textInput: task, hasFocus: true" class="todo-input">
<label>
<input type="checkbox" data-bind="checked: completed">Complete
</label>
<button data-bind="click: $root.todoList2.acceptItem" class="button">Accept Edit</button>
<button data-bind="click: $root.todoList2.revertItem" class="button">Revert Edit</button>
</div>
<div class="content-panel" data-bind="with: todoList2.itemForAdding">
<h2>Add</h2>
<input type="text" data-bind="textInput: task, hasFocus: true" class="todo-input">
<label>
<input type="checkbox" data-bind="checked: completed">Complete
</label>
<button data-bind="click: $root.saveAdd" class="button">Save</button>
<button data-bind="click: $root.cancelAdd" class="button">Cancel</button>
</div>
<div class="content-panel">
<table>
<thead>
<tr>
<th>index</th>
<th>_id</th>
<th>Task</th>
<th>Completed</th>
<th>isSelected</th>
</tr>
</thead>
<tbody data-bind="foreach: todoList2">
<tr>
<td data-bind='text: $index'></td>
<td data-bind='text: _id'></td>
<td data-bind='text: task'></td>
<td data-bind='text: completed'></td>
<td data-bind='text: _id.isSelected'></td>
</tr>
</tbody>
</table>
<!-- ko if:todoList2.messages().length -->
<div class="alert success">
<!-- ko foreach: todoList2.messages -->
<stong data-bind="text: $data.title"></stong>
<pre data-bind="text: $data.content"></pre>
<!-- /ko -->
</div>
<!-- /ko -->
</div>
<hr/>
<h2>3) Adjusted List based upon the results received from an Apollo subscription</h2>
<tasks-list params="todoList: todoList3"></tasks-list>
<!-- ko if:todoList3.messages().length -->
<div class="alert success">
<!-- ko foreach: todoList3.messages -->
<div data-bind="text: $data"></div>
<!-- /ko -->
</div>
<!-- /ko -->
<!-- example 4 with Optimistic Updates -->
<h2>4) Example showing Optimistic Updates</h2>
<p>Subscribe to changes in the ViewModel, immediately update the cache, then inform the server</p>
<!-- ko ifnot: todoList4.itemForEditing() || todoList4.itemForAdding() -->
<div class="content-panel">
<button data-bind="click: addTask4" class="button">Add Task</button>
</div>
<div class="content-panel">
<div data-bind="foreach: todoList4">
<div class="note" data-bind="click: $root.todoList4.selectItem, css: completed() ? 'complete' : 'incomplete'">
<div>
<span data-bind="click: $root.deleteItem4, clickBubble: false" class="remove">X</span>
</div>
<div data-bind="click: $root.todoList4.selectItem">
<p><span data-bind="text: task"></span></p>
<p data-bind="if: completed"><span>Completed</span></p>
</div>
</div>
</div>
</div>
<!-- /ko -->
<div class="content-panel" data-bind="with: todoList4.itemForEditing">
<h2>Editing</h2>
<input type="text" data-bind="textInput: task, hasFocus: true" class="todo-input">
<label>
<input type="checkbox" data-bind="checked: completed">Complete
</label>
<button data-bind="click: $root.acceptEditItem4" class="button">Accept Edit</button>
<button data-bind="click: $root.todoList4.revertItem" class="button">Revert Edit</button>
</div>
<div class="content-panel" data-bind="with: todoList4.itemForAdding">
<h2>Add</h2>
<input type="text" data-bind="textInput: task, hasFocus: true" class="todo-input">
<label>
<input type="checkbox" data-bind="checked: completed">Complete
</label>
<button data-bind="click: $root.saveAdd4" class="button">Save</button>
<button data-bind="click: $root.cancelAdd4" class="button">Cancel</button>
</div>
<div class="content-panel">
<table>
<thead>
<tr>
<th>index</th>
<th>_id</th>
<th>Task</th>
<th>Completed</th>
<th>isSelected</th>
</tr>
</thead>
<tbody data-bind="foreach: todoList4">
<tr>
<td data-bind='text: $index'></td>
<td data-bind='text: _id'></td>
<td data-bind='text: task'></td>
<td data-bind='text: completed'></td>
<td data-bind='text: _id.isSelected'></td>
</tr>
</tbody>
</table>
<!-- ko if:todoList4.messages().length -->
<div class="alert success">
<!-- ko foreach: todoList4.messages -->
<stong data-bind="text: $data.title"></stong>
<pre data-bind="text: $data.content"></pre>
<!-- /ko -->
</div>
<!-- /ko -->
</div>
<hr/>
</body>
</html>