0

I am trying to subscribe to a nested object, but it does not work. Here is my fiddle. below is also my code

var ViewModel = function () {
this.data = ko.observable();
var sample = {
    id: 1,
    details: {
        name: "Johnny",
        surname: "Boy",
        othername: ""
    }
};

this.data(sample);

//This does not work
// this.data().details().name.subscribe(function (val) {
    //data().details().othername(val)
// });
 };

ko.applyBindings(new ViewModel());

and here is my html

<div data-bind="with: data">
<p>Name:
    <span data-bind="text: details.name"></span>
</p>
<p>Surname:
    <input data-bind='value: details.surname' />
</p>
<p>Other:
    <input data-bind='value: details.othername' />
</p>

jpo
  • 3,959
  • 20
  • 59
  • 102
  • Is this helpful: http://stackoverflow.com/questions/7499133/mapping-deeply-hierarchical-objects-to-custom-classes-using-knockout-mapping-plu/7535397#7535397 – HJ05 Nov 07 '14 at 17:30
  • 1
    Use the official [mapping plugin](http://knockoutjs.com/documentation/plugins-mapping.html) or the more powerful [Knockout Viewmodel plugin](http://coderenaissance.github.io/knockout.viewmodel/). – Tomalak Nov 07 '14 at 17:33

1 Answers1

0

You'll need to make your nested properties observable.

Example:

name: ko.observable("Johnny")

Khan
  • 17,904
  • 5
  • 47
  • 59