Updating objects from a drop down list in knockout

2019-02-20 20:59发布

问题:

Trying to edit objects in a drop-down list. When selecting an item, name and suffix display in editable text fields; I can edit (and save) name, but changes to suffix don't stick.

Here's the JS code, the rest in on fiddle: http://jsfiddle.net/raffian/4kXXc/1/

function Domain(n){
    var self = this;
    self.name = ko.observable(n);
    self.suffix = ko.observable();
};

function DomainsViewModel(){
    var self = this;
    self.domains = ko.observableArray([]);
    self.newDomain = ko.observable("");
    self.selectedDomain = ko.observable();

    self.addNewDomain = function() {
        self.domains.push(new Domain(self.newDomain()));
        self.newDomain('')
    };
};

ko.applyBindings(new DomainsViewModel());

回答1:

Don't know the technical reason why it doesn't work (anybody knows?)

A working solution is to remove selectedDomain(). and replace it by placing a with binding in either tr or td elements data-bind="with: selectedDomain"

Sample http://jsfiddle.net/f42xw/