si2030
si2030

Reputation: 4035

Aurelia - Typescript - Trouble with setting single value in a complex data structure

I have been having all sorts of trouble with this.

I have a class "client.ts" that is instantiated which has another class as part of its structure - "address.ts". In Address I have "addressLocation.ts" and its here I need to set "stateShortName" to "VIC" I need to set a value on that class and I am having trouble with this.

The client class is:

    import { Serializable } from "../../../../services/serializable/Serializable"

    import { AddressDetails } from "../address/addressDetails"
    import { JobDetails } from "../../jobs/jobDetail/jobDetails"

    export class ClientDetails implements Serializable<ClientDetails> {

        clientId: number;
        clientNo: number;
        company: boolean;
        companyName: string;
        abn: string;
        isWarrantyCompany: boolean;
        requiresPartsPayment: boolean;
        clientFirstName: string;
        clientLastName: string;
        email: string;
        mobilePhone: string;
        phone: string;
        notes: string;

        address: AddressDetails = "new AddressDetails";

        jobs: JobDetails[];

        bankName: string;
        bankBSB: string;
        bankAccount: string;
        active: boolean;
        deActivated: string;
        activity: boolean;

        creatorId: number;
        creatorName: string;
        dateCreated: string;

        updatorId: number;
        updatorName: string;
        dateUpdated: string;

        deserialize(input) {

            this.clientId = input.clientId;
            this.clientNo = input.clientNo;
            this.company = input.company;
            this.companyName = input.companyName;
            this.abn = input.abn;
            this.isWarrantyCompany = input.isWarrantyCompany;
            this.requiresPartsPayment = input.requiresPartsPayment;
            this.clientFirstName = input.clientFirstName;
            this.clientLastName = input.clientLastName;
            this.email = input.email;
            this.mobilePhone = input.mobilePhone;
            this.phone = input.phone;
            this.notes = input.notes;
            this.bankName = input.bankName;
            this.bankBSB = input.bankBSB;
            this.bankAccount = input.bankAccount;
            this.active = input.active;
            this.deActivated = input.deActivated;
            this.activity = input.activity;
            this.creatorId = input.creatorId;
            this.creatorName = input.creatorName;
            this.dateCreated = input.dateCreated;
            this.updatorId = input.updatorId;
            this.updatorName = input.updatorName;
            this.dateUpdated = input.dateUpdated;

            if (input.jobs) {

                this.jobs = new Array<JobDetails>();

                for (let count = 0; count < input.jobs.length; count++) {

                    let job = new JobDetails;

                    this.jobs.push(job.deserialize(input.jobs[count]));
                }
            }

            if (input.address) {

                this.address = new AddressDetails;

                this.address.deserialize(input.address);
            }

            return this;
        }

        deserializeAddressStateShortName(input) {

        }
    }

It has the address class set as "new AddressDetails".

The address class is as follows:

    import { Serializable } from "../../../../services/serializable/Serializable"

    import { AddressLocation } from "./addressLocation"


    export class AddressDetails implements Serializable<AddressDetails> {
        address1?: string;
        address2?: string;

        addressLocation: AddressLocation;

        deserialize(input) {
            //console.log("INPUT: ", input)
            this.address1 = input.address1;
            this.address2 = input.address2;

            if (input.addressLocation) {

                this.addressLocation = new AddressLocation;

                this.addressLocation.deserialize(input.addressLocation);
            }

            return this;
        }
    }

Address has "addressLocation" set as "new AddressLocation"

    import { Serializable } from "../../../../services/serializable/Serializable"


    export class AddressLocation implements Serializable<AddressLocation> {
        addressLocationId?: number;
        suburb?: string;
        postcode?: string;
        stateShortName?: string;

        set stateName(name: string) {
            this.stateShortName = name;

        }

        deserialize(input) {
            this.addressLocationId = input.addressLocationId;
            this.suburb = input.suburb;
            this.postcode = input.postcode;
            this.stateShortName = input.stateShortName;

            return this;
        }
    }

each one can be initialized using the "deserialized" function.

I do a fetch and I use the data from the fetch in "client" which then populates "address" and subsequently "addressLocation".

Fine. It works when I fetch and the client object is set using this.client.deserialize(data) and the object is populated.

I dont want to go through the entire set of values just to set one value in addressLocation.

How do I, once I have created client using client = new ClientDetails();, set the value of "stateShortName" in this.client.address.addressLocation.stateShortName? I dont know how to just poplutate that value?

Upvotes: 0

Views: 57

Answers (1)

MP24
MP24

Reputation: 3200

If I understand your question correctly, you want to assign a value in the member object after initializing a new class and before deserializing anything. In this case, your class member definition must be address = new AddressDetails; without the "" signs. At the moment, your member variable is a string.

Upvotes: 1

Related Questions