1

我有嵌套的可观察数组的 Knockout.js 视图模型。

   function ParentVM(data) {
        var self = this;

        self.childs= ko.observableArray([]);
        ko.mapping.fromJS(data, mapping, this);
    }

    function ChildVM(data) {
        var self = this;

        self.propertyA = ko.observableArray([]);
        self.propertyB = ko.observable();

        ko.mapping.fromJS(data, mapping, this);
    }

    function GrandChildVM(data) {
        var self = this;

        self.propertyX = ko.observable();
        self.propertyY = ko.observable();
        self.propertyZ = ko.observable();

        ko.mapping.fromJS(data, mapping, this);
    }

我有以下绑定:

 data-bind="value: propertyX, valueUpdate: 'keydown'"

现在,当这些属性发生变化时,如何添加事件处理程序来调用我的 REST 端点来更新视图模型状态?

4

1 回答 1

1

如果我对您的理解正确,您可以这样做:

function ChildVM(data) {
    var self = this;

    self.propertyA = ko.observableArray([]);
    self.propertyB = ko.observable();

    ko.mapping.fromJS(data, mapping, this);

    self.propertyA.subscribe(function (newValue) { /* call you're api with new value! */ });
    self.propertyB.subscribe(function (newValue) { /* call you're api with new value! */ });
}
于 2012-07-27T10:48:12.497 回答