清除所有未更新视图的项目

时间:2016-10-16 02:07:55

标签: html knockout.js

我正在开发一个Web应用程序,我删除所有对web api的调用,但是在刷新页面之前视图没有更新

视图模型

$(document).ready(function () {
        ko.applyBindings(new UsersViewModel());
    });


function UsersViewModel() {

    var self = this;
    var baseUri = "http://localhost:59161/api/customers";
    self.users = ko.observableArray(); 

    self.create = function (formElement) {
        $.post(baseUri, $(formElement).serialize(), null, "json")
            .done(function (o) {
                self.users.push(o);
            });
    }
    self.update = function (user) {
        $.ajax({
            type: "PUT",
            url: baseUri,
            data: user
        });
    }
    self.remove = function () {

        $.ajax({
            type: "DELETE",
            url: baseUri 
        }).done(function () {
            self.users.remove();
        });
    }

    $.getJSON(baseUri, self.users);
}

HTML

 <table>
            <tr>
                <td><input type="button" value="Remove All" data-bind="click: $root.remove" /></td>
            </tr>
        </table>
        <table>
            <thead>
                <tr>

                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Address</th>


                </tr>
            </thead>
            <tbody data-bind="foreach: users">
                <tr>
                    <td data-bind="text: $data.FirstName"></td>
                    <td data-bind="text: $data.LastName"></td>
                    <td data-bind="text: $data.Address"></td>

                </tr>
            </tbody>

        </table>

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我的坏,我没有从可观察数组中删除所有

self.remove = function () {               
                $.ajax({
                    type: "DELETE",
                    url: "/api/users"
                }).done(function () {
                    self.users.removeAll();
                });
            }