2016-04-11 26 views
1

angular2/http'u kullanarak uzak bir sunucuya http çağrısı yapıyorum ve bu çok işe yaramaz bir şekilde çalışıyor. Ama geri döndüğü gözlenebilirleri kullanmakta sorun yaşıyorum.Abone olmadan önce işlevleri çağırma

http çağrılarımı yaptığımda 3 farklı bileşenden geçiyorum.

ben onun mümkün yapmak bilmek istiyorum ne

(aynı dışarı çalışır olarak kolaylığı için buraya hasta sadece 3 farklı yöntem yerine çağrı yapmak), kullanım aynı gözlemlenebilir veya eğer iki kez abone orada bazı işlevleri çağırabilmem için bir yoldur, böylece bir http araması yaptığım her yere eklemekten ziyade istekte kayıt ekleyebilirim. Ben kullanıldı url çıktısını sunucu veya herhangi bir hata alınan verileri yazdırmak için console.log çağırmak isterler request yöntemde

request(type: RequestMethod, url: string, data: any) { 

    let params: URLSearchParams = new URLSearchParams(); 
    let req: RequestOptions; 
    let headers = new Headers(); 

    for (let key in data) { 
     params.set(key, data[key]); 
    } 

    if (type === 0) { 
     req = new RequestOptions({ 
     method: type, 
     search: params 
     }); 
    } else { 
     headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
     req = new RequestOptions({ 
     method: type, 
     body: params.toString(), 
     headers: headers 
     }); 
    } 

    console.log('Http Request: ' + url); 
    console.log(req); 
    return this.http.request(this.testUrl + url, req) 
     .map((res: Response) => res.json()); 
     // .subscribe(         
     // data => {         
     //  console.log('Data Return for ' + url); 
     //  console.log(data.data); 
     //  // return data; //i removed this subscribe so i can 
     // },    //pass the map back through and 
     // err => {   //use the subscribe later in the initial call 
     //  this.logError(url, err); 
     // }, 
     // () => { 
     //  if (afterSuccess) { 
     //  afterSuccess(); 
     //  } 
     //  console.log('Completed '+ url); 
     // } 
     //); 
    } 

    logError(url: string, err: any) { 
    console.log('Error in call: ' + url); 
    console.log(err); 
    } 

    get(url: string, data: any) { 
    return this.request(RequestMethod.Get, url, data); 
    } 

    post(url: string, data: any) { 
    return this.request(RequestMethod.Post, url, data); 
    } 

    emailExists(email_address: string, user_type?: string) { 
    let data: any = {}; 

    data.email_address = email_address; 
    data.service_provider_id = this.service_provider_id; 

    if (user_type) { 
     data.user_type = user_type; 
    } 

    return this.get('emails/email-address/exists', data); 
    } 

    ngOnInit() { 
    this.emailExists('[email protected]').subscribe(data => { 
     this.email = data.data; 
     console.log('in Signin'); 
     console.log(this.email); 
    }, err => { 
     console.log(err); 
    },() => { 

    }); 
    } 

. emailExists()ngOnInit() numaralı telefondaki aboneliğin başarı ve hata bölümlerine ekleyebileceğimi biliyorum, ancak bu, uygulama boyunca her bir aramaya bunları eklemem gerekeceği anlamına gelir.

Yardımlarınız ve zamanınız için şimdiden teşekkür ederiz.

cevap

1

Bunu yapmak için do, catch ve finally operatörlerinden yararlanılabilir.

return this.http.request(this.tpayUrl + url, req) 
    .map((res: Response) => res.json()) 
    .do(data => {         
     console.log('Data Return for ' + url); 
     console.log(data.data); 
    }) 
    .catch(err => { 
     this.logError(url, err); 
     return Observable.throw(err); 
    }) 
    .finally(() => { 
     if (afterSuccess) { 
     afterSuccess(); 
     } 
     console.log('Completed '+ url); 
    }); 
: Burada

bir örnek