2

我有以下代码

var Person = function() {
    firstName = ko.observable();
    lastName = ko.observable();
};

function AppViewModel() {
    var self = this;
    self.personData = ko.observable(new Person());
    self.newPerson = ko.observable(new Person());
    self.savePerson = function() {
        // ajax call
        // self.personData will be empty - why?
        console.log(ko.toJSON(self));
    };

    self.newPerson = function() {
        self.newPerson = ko.observable(new Person()); // this makes the object disappear from the model, dunno why
    };
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

我试图将“Person”绑定到“PersonData”可观察对象并在单击新按钮时重置该“PersonData”,但是当我将模型发布回系统时,它变为空。

jsfiddle在这里 http://jsfiddle.net/DiegoVieira/SKVRm/

4

1 回答 1

2

您的 newPerson 函数有错误,语法正确

self.personData(new Person());

http://jsfiddle.net/SKVRm/3/

编辑:您分配了错误的成员,并且您还被重新分配了一个新的可观察对象,而不是更新旧的可观察对象,即通过绑定绑定到 personData 的对象

于 2013-01-22T15:31:27.953 回答