javascript - Windows Azure + DevExpress(手机)获取待办事项列表(标准示例)

标签 javascript azure knockout.js phonejs devextreme

我开始学习azurephonejs。 通过一个标准示例来获取待办事项列表:

$(function() {
var client = new WindowsAzure.MobileServiceClient('https://zaburrito.azure-mobile.net/', 'key');
var 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 $(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();

});

并且它有效! 改成使用phonejs,程序停止运行,即使出错也没有问题!

这是我的观点:

<div data-options="dxView : { name: 'home', title: 'Home' } " >
<div class="home-view"  data-options="dxContent : { targetPlaceholder: 'content' } " >
    <button data-bind="click: incrementClickCounter">Click me</button>
    <span data-bind="text: listData"></span>
    <div data-bind="dxList:{
            dataSource: listData,
            itemTemplate:'toDoItemTemplate'}">
        <div data-options="dxTemplate:{ name:'toDoItemTemplate' }">
            <div style="float:left; width:100%;">
                <h1 data-bind="text: name"></h1>
            </div>
        </div>
    </div>
</div>

这是我的 View 模型:

Application1.home = function (params) {
var client = new WindowsAzure.MobileServiceClient('https://zaburrito.azure-mobile.net/', 'key');
var todoItemTable = client.getTable('todoitem');

var toDoArray = ko.observableArray([
    { name: "111", type: "111" },
    { name: "222", type: "222" }]);

var query = todoItemTable.where({ complete: false });
query.read().then(function (todoItems) {
    for (var i = 0; i < todoItems.length; i++) {
        toDoArray.push({ name: todoItems[i].text, type: "NEW!" });
    }
});

var viewModel = {
    listData: toDoArray,
    incrementClickCounter: function () {
        todoItemTable = client.getTable('todoitem');
        toDoArray.push({ name: "Zippy", type: "Unknown" });
    }
};

return viewModel;

};

我可以轻松地将项目添加到程序列表中,但无法从服务器列表中添加项目:-( 我已经筋疲力尽了,三天都解决不了问题,这对我来说至关重要! 指出我的错误在哪里!谢谢你!

最佳答案

我建议您使用 DevExpress.data.DataSourceDevExpress.data.CustomStore 而不是 ko.observableArray

Application1.home = function (params) {
    var client = new WindowsAzure.MobileServiceClient('https://zaburrito.azure-mobile.net/', 'key');
    var todoItemTable = client.getTable('todoitem');

    var toDoArray = [];        
    var store = new DevExpress.data.CustomStore({
      load: function(loadOptions) {
        var d = $.Deferred();
        if(toDoArray.length) {
            d.resolve(toDoArray);
        } else { 
           todoItemTable
               .where({ complete: false })
               .read()
               .then(function(todoItems) {
                   for (var i = 0; i < todoItems.length; i++) {
                       toDoArray.push({ name: todoItems[i].text, type: "NEW!" });
                   }
                   d.resolve(toDoArray);
               });
        }
        return d.promise();
      },

      insert: function(values) {
        return toDoArray.push(values) - 1;
      },

      remove: function(key) {
        if (!(key in toDoArray))
          throw Error("Unknown key");

        toDoArray.splice(key, 1);
      },

      update: function(key, values) {
        if (!(key in toDoArray))
          throw Error("Unknown key");
        toDoArray[key] = $.extend(true, toDoArray[key], values);
      }
    });

    var source = new DevExpress.data.DataSource(store);
    // older version
    store.modified.add(function() { source.load(); });
    // starting from 14.2:
    // store.on("modified", function() { source.load(); });

    var viewModel = {
        listData: source,
        incrementClickCounter: function () {
            store.insert({ name: "Zippy", type: "Unknown" });
        }
    };

    return viewModel;

}

您可以阅读更多相关信息 herehere .

关于javascript - Windows Azure + DevExpress(手机)获取待办事项列表(标准示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117316/

相关文章:

azure - 在Azure应用程序服务中切换docker镜像

Azure VPN 客户端错误 : Failure in acquiring AAD Token: Keyset does not exist

azure - 如何在 Linux Azure 应用服务上安装数据包

javascript - MVC4 局部 View 中的 knockout 绑定(bind)

javascript - 使用 Knockout.js 进行动态模板处理

knockout.js - 如何从现有的可观察物中移除增量剂?

javascript - jQuery 在 div 中显示数组

javascript - 通过ajax引入xslt页面后执行javascript

相当于 jquery ajaxStop 的 Javascript

javascript - 在 xslt 文件中使用 javascript