javascriptknockout.jscomputed-observable

How to bind property on parent object with knockoutjs?


I'm developing a application with knockout.js framework. I have one viewmodel like that:

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 obtain one exception like that:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'CountryCode'

Can you help me to resolve my problem?

Thank you a lot, Marco


Solution

  • I resolved my problem using subscribing feature of knockout.

    Now my code is like that:

    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 CountryCode property is changed.

    Thanks