-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
104 lines (84 loc) · 3.57 KB
/
page.js
File metadata and controls
104 lines (84 loc) · 3.57 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
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
$(function() {
var client = new WindowsAzure.MobileServiceClient('https://mobilenetgio.azure-mobile.net/', 'rlowNvqSexjEddvPtbdmVHGBBiXcBS21'),
todoItemTable = client.getTable('todoitem');
// Read current data and rebuild UI.
// If you plan to generate complex UIs like this, consider using a JavaScript templating library.
function refreshTodoItems() {
var query = todoItemTable.where({ complete: false });
query.read().then(function(todoItems) {
var listItems = $.map(todoItems, function(item) {
return $('<li>')
.attr('data-todoitem-id', item.id)
.append($('<button class="item-delete">Delete</button>'))
.append($('<input type="checkbox" class="item-complete">').prop('checked', item.complete))
.append($('<div>').append($('<input class="item-text">').val(item.text)));
});
$('#todo-items').empty().append(listItems).toggle(listItems.length > 0);
$('#summary').html('<strong>' + todoItems.length + '</strong> item(s)');
}, handleError);
}
function handleError(error) {
var text = error + (error.request ? ' - ' + error.request.status : '');
$('#errorlog').append($('<li>').text(text));
}
function getTodoItemId(formElement) {
return Number($(formElement).closest('li').attr('data-todoitem-id'));
}
// Handle insert
$('#add-item').submit(function(evt) {
var textbox = $('#new-item-text'),
itemText = textbox.val();
if (itemText !== '') {
todoItemTable.insert({ text: itemText, complete: false }).then(refreshTodoItems, handleError);
}
textbox.val('').focus();
evt.preventDefault();
});
// Handle update
$(document.body).on('change', '.item-text', function() {
var newText = $(this).val();
todoItemTable.update({ id: getTodoItemId(this), text: newText }).then(null, handleError);
});
$(document.body).on('change', '.item-complete', function() {
var isComplete = $(this).prop('checked');
todoItemTable.update({ id: getTodoItemId(this), complete: isComplete }).then(refreshTodoItems, handleError);
});
// Handle delete
$(document.body).on('click', '.item-delete', function () {
todoItemTable.del({ id: getTodoItemId(this) }).then(refreshTodoItems, handleError);
});
// On initial load, start by fetching the current data
//refreshTodoItems();
function refreshAuthDisplay() {
var isLoggedIn = client.currentUser !== null;
$("#logged-in").toggle(isLoggedIn);
$("#logged-out").toggle(!isLoggedIn);
if (isLoggedIn) {
$("#login-name").text(client.currentUser.userId);
refreshTodoItems();
}
}
function logInFB() {
client.login("facebook").then(refreshAuthDisplay, function (error) {
alert(error);
});
}
function logInTwitter() {
client.login("twitter").then(refreshAuthDisplay, function (error) {
alert(error);
});
}
function logOut() {
client.logout();
refreshAuthDisplay();
$('#summary').html('<strong>You must login to access data.</strong>');
}
// On page init, fetch the data and set up event handlers
$(function () {
refreshAuthDisplay();
$('#summary').html('<strong>You must login to access data.</strong>');
$("#fb").click(logInFB);
$("#twitter").click(logInTwitter);
$("#logged-in button").click(logOut);
});
});