我有以下代码
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/