2017-08-22 12 views
5

Jestem nowicjuszem pojęć obserwowalnych i potrzebuję pomocy przy konwersji.
Mam usługę, która zwraca Observable<Response> z żądania Http, ale muszę go przekonwertować do Observable<PriceTag>, aby go użyć na DataSource wewnątrz metody connect.
Czy mimo to można to zrobić?Angular 4, konwersja odpowiedzi http obserwowalnej do obserwowanego obiektu

Jest to metoda z moich usług:

getPriceTags(): Observable<Response> { 

    // Set the request headers 
    const headers = new Headers({ 'Content-Type': 'application/json' }); 

    // Returns the request observable 
    return this.http.post(Constants.WEBSERVICE_ADDRESS + "/priceTag", null, {headers: headers}); 

} 

I tu jest klasa DataSource gdzie muszę zwrócić go jako Observable<PriceTag>:

export class PriceTagDataSource extends DataSource<PriceTag> { 

    constructor (private priceTagService: PriceTagService) { 
     super(); 
    } 

    connect(): Observable<PriceTag> { 

     // Here I retrieve the Observable<Response> from my service 
     const respObs = this.priceTagService.getPriceTags(); 

     // Now I need to return a Observable<PriceTag> 

    } 

    disconnect() {} 

} 

Oto przykład z odpowiedzi od mojego żądanie:

{ 
    // This object is used to check if the query on the server was sucessful 
    "query": { 
     "sucessful": true 
    }, 

    // These are my PriceTags 
    "tags": [ 
     { 
      "id": "1", 
      "name": "MAIN" 
     }, 
     { 
      "id": "2", 
      "name": "CARD" 
     } 
    ] 
} 
+0

Czy jedną z odpowiedzi pomóc? –

Odpowiedz

13

Od kąta 4.3 można to zrobić automatycznie.

Przykład:

export class SomeService { 
    constructor(private http: HttpClient) {} // <--- NOTE: HttpClient instead of Http 

    getSome(): Observable<MyAwesomeObject> { 
     return this.http.get<MyAwesomeObject>.get('myUrl'); 
    } 
} 

Tak więc w Twoim przypadku to będzie:

return this.http.post<PriceTag>(Constants.WEBSERVICE_ADDRESS + "/priceTag", null, {headers: headers});

Ponownie użyć HttpClient zamiast Http

+1

Oooo nie wiedziałem, że może to zrobić ... schludny – diopside

+0

jest jakaś różnica między tym a 'getSome(): obserwowalnych { zamian tego. http.get ("myUrl") map ((odpowiedź) => response.json()); } ' –

+0

@RobinDijkhof Wygląda na to, że Pritesh pyta, JEŚLI istnieje różnica, ponieważ zaczyna zdanie:" czy istnieje różnica ..."a nie" jest różnica ... " –

-3

wystarczy zmienić Observable<Response> na Observable<PriceTag>

Tego typu odlewy są przeznaczone do maszynopisu, aby upewnić się, że wysyłasz/oczekujesz odpowiednich typów.

EDIT

import { PriceTag } from 'your-path-to-model'; 

getPriceTags(): Observable<Response> { 
    // Set the request headers 
    const headers = new Headers({ 'Content-Type': 'application/json' }); 

    // Returns the request observable 
    return this.http.post(Constants.WEBSERVICE_ADDRESS + "/priceTag", null, {headers: headers}) 
    .map((res: Response) => { 
     return new PriceTag(res.json()); 
    }); 
} 
+3

To jest takie złe, ponieważ obecna implementacja nie zwróci PriceTag. – Boland

2

Chyba odpowiedź HTTP jest JSON zawierający pricetag? Problem polega na tym, że chcesz utworzyć obiekt PriceTag. Możesz po prostu przekonwertować json na PriceTag przez odlewanie typu, ale wtedy nie będzie to prawdziwy obiekt PriceTag.

Sposób, w jaki zostały rozwiązane to:

export class Serializable { 
    constructor(json?: any) { 
    if (json) { 
     Object.assign(this, json); 
    } 
    } 
} 

A potem serializable klasa:

export class PriceTag extends Serializable {} 

Następnie czynność GetPriceTags musi być zmienione na:

getPriceTags(): Observable<PriceTag> { 

    // Set the request headers 
    const headers = new Headers({ 'Content-Type': 'application/json' }); 

    // Returns the request observable 
    return this.http.post(Constants.WEBSERVICE_ADDRESS + "/priceTag", null, {headers: headers}) 
    .map(res => new PriceTag(res.json())); 

} 
+0

miłujcie „który jest tak źle komentarz” btw – Sonicd300

Powiązane problemy