1

I have Knockout.js view model that has nested observable arrays.

 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); } 

I have following bindings:

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

Now, how can I add event handler to call my REST endpoint to update view model state when these properties changes?

1 Answer 1

1

If I understand you correctly, this is what you can do:

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! */ }); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but this does not seem to work. The subscribed callback does not get called.
That doesn't make any sense, is the model bound correctly? Do any values in the parameters get shown in the UI?
Thanks, the problem was that when the data was initialized it did not use the provided mapping. Changing ko.mapping.fromJS(data) to ko.mapping.from(data, mapping) fixed the issue and subscribing to properties works.
Please make sure to mark me answer as the correct one, if it solved your problem. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.