How to bind properties of parent object using knockout js?


alarm

I am developing an application using knockout js framework. I have a viewmodel like this:

var MyViewModel= {
    Id: ko.observable(),
    CountryCode: ko.observable(),
    NormalizedAddress:
        {
            COUNTRY_CODE: ko.computed(function () { return this.CountryCode(); }),
            Street: ko.observable(),
            ZipCode: ko.observable(),
            AreaCode: ko.observable(),
            Town: ko.observable(),
            Description: ko.observable()
        }

When I run my app I get a similar exception:

0x800a01b6 - JavaScript Runtime Error: Object doesn't support property or method 'CountryCode'

Can you help me with my problem?

Many thanks Marco

alarm

I solved my problem using Knockout's subscription feature .

Now my code is like this:

var MyViewModel= {
    Id: ko.observable(),
    CountryCode: ko.observable(),
    NormalizedAddress:
        {
            COUNTRY_CODE: ko.observable(),
            Street: ko.observable(),
            ZipCode: ko.observable(),
            AreaCode: ko.observable(),
            Town: ko.observable(),
            Description: ko.observable()
        }
}

MyViewModel.CountryCode.subscribe(function (newValue) {
    MyViewModel.NormalizedAddress.COUNTRY_CODE(newValue);
});

So I can change the value when the CountryCode property changes.

thanks

Related


How to bind multiple images to graphic elements using knockout.js

happy I would like to use knock to generate the output below. <figure data-bind="?"> <img src="a.jpg" /> <img src="b.jpg" /> <img src="c.jpg" /> ...... ....... <figcaption>Image title</figcaption> </figure> <img />There are many elements , only one figcaption

How to bind multiple images to graphic elements using knockout.js

happy I would like to generate the output below with gene knockout. <figure data-bind="?"> <img src="a.jpg" /> <img src="b.jpg" /> <img src="c.jpg" /> ...... ....... <figcaption>Image title</figcaption> </figure> There are many <img />elements, only one figca

How to bind multiple images to graphic elements using knockout.js

happy I would like to generate the output below with gene knockout. <figure data-bind="?"> <img src="a.jpg" /> <img src="b.jpg" /> <img src="c.jpg" /> ...... ....... <figcaption>Image title</figcaption> </figure> There are many <img />elements, only one figca

How to access properties of bound object instance using Knockout

Mike I can't figure out how to get the properties of the bound item. The following does not work when .name() exists. <span data-bind='text: selectedMenuSection().name()'></span> It seems that you cannot access the properties of the bound object in data bindi

How to access properties of bound object instance using Knockout

Mike I can't figure out how to get the properties of the bound item. The following does not work when .name() exists. <span data-bind='text: selectedMenuSection().name()'></span> It seems that you cannot access the properties of the bound object in data bindi

How to access properties of bound object instance using Knockout

Mike I can't figure out how to get the properties of the bound item. The following does not work when .name() exists. <span data-bind='text: selectedMenuSection().name()'></span> It seems that you cannot access the properties of the bound object in data bindi

How to get parent form id using knockout js?

Rakesh Jesadiya How to get form id or form class name using knockout js? <span data-bind="text: $parent.id"></span> I would like to use ko in below position to form ID or class name. <form class="form-shipping-address" id="shipping-form" data-bind="attr: {'dat

How to conditionally bind to "valueUpdate" in knockout.js?

Beard Tony I'm currently using the knockout.js and knockout.validation.js plugins and they work well in most cases. What I've been struggling with is getting the validation to fire the first time when the input is fuzzed, and then every time on a per-keystroke

How to bind controls to object properties?

sab669 Actually, I've never used WPF before, only WinForms, and I'm trying to follow this tutorial , but the sample code is really bad. Incomplete excerpts and plenty are left to the reader to deduce where everything goes. It's talking about adding data bindin