Reputation: 1150
I have the following code
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());
where I'm trying to bind a "Person" to a "PersonData" observable and reset that "PersonData" when the new button is clicked, but when I post the model back to the system, it goes empty.
jsfiddle here http://jsfiddle.net/DiegoVieira/SKVRm/
Upvotes: 2
Views: 91
Reputation: 17554
Your newPerson function has a bug, correct syntax
self.personData(new Person());
edit: You were assignig the wrong member, and you were also reassigned it a new observable rather than updating the old observable that is the one that is bound to the personData with binding
Upvotes: 2