我有以下代码
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”observable并在单击新按钮时重置“PersonData”,但是当我将模型发布回系统时,它会变空。
jsfiddle在这里 http://jsfiddle.net/DiegoVieira/SKVRm/
答案 0 :(得分:2)
您的newPerson函数有错误,语法正确
self.personData(new Person());
编辑:你是一个错误的成员,你也被重新分配了一个新的observable,而不是更新旧的observable,它是绑定到带绑定的personData的那个