Click here to Skip to main content
15,886,689 members
Articles / Hosted Services / Azure

Android and iOS Mobile Device Development with Azure Mobile Services using HTML5 and JavaScript

Rate me:
Please Sign up or sign in to vote.
4.82/5 (16 votes)
10 May 2013CPOL8 min read 54.1K   677   44  
Tutorial showing how to build an iOS or Android app using Azure Mobile Services with HTML5 and JavaScript in Visual Studio.
$(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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect bartread.com
United Kingdom United Kingdom
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.

Comments and Discussions