Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have scnerio where i want to make one field computed observable if check box is clicked if not then whatver user writes into it that should store into that field

Address 1 :-
Address 2 :-

Check box :- is same as above

Mailing address :-

now here what i want to do is if check box is checked then whatver user has entred should be same as above value. If not then user can enter differnt mailing address

What I have tried:

//clientViewModel.client_mailingAddress = ko.computed(function () {
// if (clientViewModel.client_isMailingAddressReadOnly()) {
// return clientViewModel.client_AddressLine1() + ' ' + clientViewModel.client_AddressLine2() + ' ' + clientViewModel.client_AddressLine3()
// }
//});

here check box works perfctl but now when check box not checked and if i enter something for my maling address that takes it as undefined..
Posted
Updated 14-Jul-16 17:46pm

1 solution

If you want to make your computed observable writable as well then you have to use purecomputed , it works as same but only differnce is here you get the access t write as well

chemistViewModel.chemist_mailingAddress = ko.pureComputed({
        read: function () {
            if (chemistViewModel.chemist_isMailingAddressReadOnly()) {
                var updatedAddress = chemistViewModel.chemist_AddressLine1() + ' ' + chemistViewModel.chemist_AddressLine2() + ' ' + chemistViewModel.chemist_AddressLine3();
                return updatedAddress;
            }
            else {
                var updatedAddress = chemistViewModel.chemist_mailingAddressWritable();
                return updatedAddress;
            }
        },
        write: function (value) {
            if (!chemistViewModel.chemist_isMailingAddressReadOnly()) {
                chemistViewModel.chemist_mailingAddressWritable(value);
            }
        },
        owner: this
    });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900