Newbie to ko, I am struggling with this. I have a form for adding a new address (for simplifying it let's use just one property), that updates an arrray (and displays it in a list), what I would like to achieve is be able to select one of the addresses in the array and be able to display it in the form, This works, but the changes there are not updating the displayed in the list.
Code inside the view:
<div data-bind="with: newAddress" class="span6">
<input data-bind="value: Line1()" placeholder="Address line 1" />
<div>
<button data-bind="click: $parent.addAddress">Add</button>
</div>
</div>
<div class="span4">
<div data-bind="visible: addresses().length > 0">
<div data-bind="foreach: addresses">
<address>
<span data-bind="click: $parent.removeAddress">Remove</span>
<span data-bind="click: $parent.editAddress">Edit</span>
<div data-bind="text: Line1"></div>
</address>
</div>
</div>
</div>
Related code in the script
function ViewModel() {
var self = this;
self.addresses = ko.observableArray(ko.utils.arrayMap(addresses, function(address) {
return new Address(address);
}));
self.newAddress = {
Line1: ko.observable(""),
};
self.addAddress = function() {
self.addresses.push(new Address(this);
self.newAddress.Line1("");
};
self.addAddress = function() {
self.addresses.push(new Address(this);
self.newAddress.Line1("");
};
self.editAddress = function() {
self.newAddress.Line1(this.Line1);
};
self.remove = function() {
self.parties.remove(this);
};
};
// As it is used for both the addresses coming from the server and the added in
// the form I do this check
function Address(address) {
this.Line1 = ko.isObservable(address.Line1) ? address.Line1() : address.Line1;
}
How could I achieve to bind the changes in the form when one of the addresses selected to the data displayed in the list?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…