[英]KnockoutJS - Rebinding ViewModel
I want to switch a object in a viewModel with an other, with the same type (eg Person). 我想在viewModel中切换一个具有相同类型的其他对象(例如Person)。 If i do this: 如果我这样做:
var personViewModel = function (person) {
var self = this;
self.id = person.id;
self.firstName = ko.observable(person.firstName);
self.lastName = ko.observable(person.lastName);
self.addresses = ko.observableArray(contact.addresses);
self.removeAddress = function (address) {
self.addresses.remove(address);
}
}
and bind it with: 并绑定它:
ko.applyBindings(new personViewModel(person), $("#person")[0]);
it works great on the first time, but if i bind it with a other object on a second time, the first binding will not disappear. 它第一次工作得很好,但是如果我第二次将它与另一个对象绑定,第一次绑定就不会消失。
How can i switch easily the object person in my viewModel? 如何在viewModel中轻松切换对象人?
You would basically want your entire view model to be observable, then swap in a new personViewModel object. 您基本上希望整个视图模型是可观察的,然后交换一个新的personViewModel对象。 Would be like: 就像:
var viewModel = {
person: ko.observable()
};
viewModel.person(new personViewModel(person));
ko.applyBindings(viewModel);
Then, just swap a new person in like: 然后,只需交换一个新人,如:
viewModel.person(new personViewModel(newPerson));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.