How to associate a property with a parent object with knockoutjs?

I am developing an application using the knockout.js framework. I have one view model:

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 application, I get one exception:

0x800a01b6 - JavaScript runtime error: the object does not support the property or method 'CountryCode'

Can you help me solve my problem?

Thank you so much Marco

+4
source share
1 answer

I solved my problem using a knockout subscription feature.

Now my code is as follows:

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

Therefore, I can change the value when changing the CountryCode property.

thank

0
source

All Articles