Restangular OneToOne connection

Issue

At #/dossiers/{id}/offertes of my client application
I need to get my dossier from rest api/dossiers/{id}.
Then on that dossier I need to get the corresponding offerte api/dossiers/{id}/offerte
From that I will get my offerteRecords api/offertes/{id2}/records

I wil include my REST data here

The first dossier is no problem.

restService.getOne("dossiers",this.parentId).then(
    (dossier: any): void => {
        this.dossier = dossier;
}

But then from that I want to get my second dossier.
I’m able to do it like this:

restService.getOneUrl("offertes", this.dossier._links.offerte.href).then(
    (offerte: any): void => {
        this.parent = offerte;
        console.log("offerte is ", this.parent);
    }
);

And this gives me

Object {id: 1, createdBy: “system”, createdOn: “2016-05-17T14:24:43.384+0000”, updatedBy: null, updatedOn: null…}

But I want to do it something like

this.dossier.getList("offerte").then(
            (offerte: any):void =>{
                console.log("offerte is ", offerte);
            }
        );

But this returns

[route: “offerte”, reqParams: null, restangularized: true, fromServer: true, parentResource: Object…]

How should I call on this object instead of getList()?

Solution

I’ve found a partial solution to my problem.
In my restangular configuration there was this pre-defined when I got the project.

restangularProvider.addResponseInterceptor(function(data: any, operation: string, what: string,
                                                    url: string, response: any,
                                                    deferred: any): any {
    let result: any = data;

    if (operation === 'getList') {
        if (data._embedded !== undefined) {
            result = data._embedded[what];
        } else {
            result = [];
        }
}

    return result;
});

This made the wonky formatting on my received data compared to the getOneUrl.
This was done because restangular always wants an array to input to getList.
I could solve this by writing some certain of custom wrapper that satisfies the requirements of getList but the easiest/fastest solution to do what I want is use the customGET();

this.dossier.customGET("offerte").then(
        (offerte: any):void =>{
            console.log("offerte is ", offerte);
        }
    );

This returns the correct offerte object.

Answered By – turoni

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.