|
$(function () {
var client = new WindowsAzure.MobileServiceClient('https://bjrtodolist.azure-mobile.net/', 'tRiEWWrDmHlZtrqxkAZvqHgNAjctXY75'),
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)');
});
}
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);
}
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 });
});
$(document.body).on('change', '.item-complete', function () {
var isComplete = $(this).prop('checked');
todoItemTable.update({ id: getTodoItemId(this), complete: isComplete }).then(refreshTodoItems);
});
// Handle delete
$(document.body).on('click', '.item-delete', function () {
todoItemTable.del({ id: getTodoItemId(this) }).then(refreshTodoItems);
});
// On initial load, start by fetching the current data
refreshTodoItems();
});
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Bart is a web and database performance consultant specialising in .NET, SQL Server, and JavaScript across both desktop and mobile platforms.
He is the author and main contributor of the
Dapper.SimpleLoad and
Dapper.SimpleSave extensions for StackExchange's
Dapper microORM, both of which are available on
Nuget.
Formerly he spent almost 10 years at
Red Gate Software, with several years in the company's .NET Developer Tools division leading the development team that built
ANTS Performance Profiler (Red Gate's .NET and SQL Server performance profiling tool), and
ANTS Memory Profiler (their .NET memory profiler). He also lead development of well known tools such as
SQL Prompt and
.NET Reflector.
He can be found at
www.bartread.com, and maintains a free online games site at
arcade.ly, where he hosts HTML5, JavaScript, and CSS versions of classic arcade games such as Star Castle (his version is called
Star Citadel) that he uses to illustrate principles of high performance client-side development.