淘汰推送没有更新GUI

时间:2012-11-06 15:24:59

标签: knockout.js viewmodel

当我向observableArray添加项目时,我正在使用淘汰赛并获得UI更新。初始列表和deleteEmployee工作正常并更新UI appropriatley但是当我尝试添加员工时,UI根本就不会更新。下面是viewmodel和html。感谢我能得到的任何帮助。     
    
视图模型:

function viewModel() {
        var self = this;
        self.employees = ko.observableArray();

        self.deleteEmployee = function () {
            var employee = this;
            dataservice.del(this.id, function () {
                self.employees.remove(employee);
            })
        },

        self.addEmployee = function () {
            var emp = {};
            emp.Name = $("#newName").val();
            emp.Email = $("#newEmail").val();
            dataservice.add(emp, function (data) {
                var employee = new Employee(data.id, emp.Name, emp.Email);
                self.employees.push(employee);
            });
        }.bind(self);

        $.each(data.rows, function (index, row) {
            self.employees.push(new Employee(row.value.Id, row.value.Name, row.value.Email));
        });

        return {
            employees: self.employees,
            deleteEmployee: self.deleteEmployee,
            addEmployee: self.addEmployee
        }
    };

    ko.applyBindings(new viewModel());



HTML

<article data-bind="foreach: employees">
                <div class="emp">
                    <div class="empTop">
                        <span data-bind="text: name"></span>
                        <div class="del" data-bind="click: $parent.deleteEmployee, clickBubble: false"></div>
                    </div>
                    <div class="empContent">
                        <div class="row">
                            <label for="name">Name</label>
                            <input name="name" type="text" data-bind="value: name">
                        </div>
                        <div class="row">
                            <label for="name">Email</label>
                            <input name="email" type="text" data-bind="value: email">
                        </div>
                        <div class="row">
                            <input type="button" value="Update">
                        </div>
                    </div>
                </div>                 
            </article>

            <aside>
                <h3>Create</h3>
                <div>
                    <div class="row">
                        <label for="newName">Name</label>
                        <input id="newName" name="newName" type="text">
                    </div>
                    <div class="row">
                        <label for="newEmail">Email</label>
                        <input id="newEmail" name="newEmail" type="text">
                    </div>
                    <div class="row">
                        <input type="button" value="Create" data-bind="click: addEmployee">
                    </div>
                </div>
            </aside>

1 个答案:

答案 0 :(得分:1)

似乎问题不在于Knockout。我把你的代码放到JSFiddle中,在javascript中注释了几行,但是没有触及你的HTML,并且创建工作正常。

看看小提琴:http://jsfiddle.net/mshmelev/ZF5pC/

所以,问题可能是:

  1. dataservice.add()函数未调用您的回调
  2. 你的Employee课有些问题(我在小提琴里创造了一个存根)