-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo-client.js
More file actions
76 lines (68 loc) · 2.47 KB
/
todo-client.js
File metadata and controls
76 lines (68 loc) · 2.47 KB
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
var socket = io();
// Finds the element that has a data-todo-id="todo._id" property
function findElement(todo) {
return $('#todos').find('[data-todo-id="' + todo._id + '"]')
}
// Returns the data-todo-id property for the parent li element
function getId(element) {
return $(element).parents('li').data('todo-id');
}
// A general callback for service methods that just alerts
// the error if one happened
function errorHandler(error) {
if(error) {
alert('There was an error: ' + error.message);
}
}
$('#todos').on('submit', 'form.create-todo', function(ev) {
// Find the todo text form field
var field = $(this).find('[name="description"]');
// Call todos.create(data, {}, callback)
socket.emit('todos::create', {
text: field.val(),
complete: false
}, {}, errorHandler);
// Reset the value
field.val('');
// Prevent actual form submission
ev.preventDefault();
}).on('click', '.delete', function(ev) {
var id = getId(this);
// Call todos.remove(id, {}, callback)
socket.emit('todos::remove', id, {}, errorHandler);
ev.preventDefault();
}).on('click', '[name="complete"]', function() {
var id = getId(this);
// Complete status is the checkbox status
var complete = $(this).is(':checked');
// patch will merge existing data (udpate replaces everything)
// Call todos.patch(id, { complete: status }, {}, callback)
socket.emit('todos::patch', id, {
complete: complete
}, {}, errorHandler);
});
socket.on('todos created', function(todo) {
// Create the HTML for the new list element. This can be done nicer
// eventually by using a view engine like EJS in the browser and sharing
// the template from index.ejs
var html = '<li class="page-header checkbox" data-id="' + todo._id + '">' +
'<label><input type="checkbox" name="done">' +
todo.text +
'</label><a href="javascript://" class="pull-right delete">' +
'<span class="glyphicon glyphicon-remove"></span>' +
'</a></li>';
// Add it to the todo list
$('.todos').append(html);
});
// Listen to a patched todo
socket.on('todos patched', function(todo) {
var element = findElement(todo);
// Find the checkbox element
var checkbox = element.find('[name="complete"]');
// Set the checked property to the todo complete status
checkbox.prop('checked', todo.complete);
});
socket.on('todos removed', function(todo) {
// Find the element and remove it
findElement(todo).remove();
});