简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM