2016-04-06 32 views
1

Angular2 ve rxjs kullanıyorum.RxJS, ilk başarılı olduğunda ikinci işlemi çağırıyor

Giriş() adı verilen bir işlem var. Bu, sunucuya kimlik doğrulama ayrıntılarını göndermek için bir http.post isteği kullanır ve sonra bir jetonu geri alır.

Sonucu okumak gerekiyorsa ve belirtecin başarılı bir şekilde alınması durumunda, belirtecin doğrulanması ve kodunun çözülmesi için bazı işlemler yapılır ve bunların tümü tamamsa, kullanıcı adı belirtecinden sunucuya gönderilir. Bir http.get ve kullanıcının bilgilerini al.

Yukarıdakilerin hepsinin bir Gözlemlenebilir olarak döndürülmesini istiyorum, ancak kafamı, diğerinin birbiri ardına gerçekleşmesi gereken iki işlemin RxJS yolu kullanılarak nasıl yapılandırılacağı konusunda çiziyorum.

İlk harekata abone olduğumu ve ardından ikinci işlemi ilk önce "doğru" yoldan aradığını sanmıyorum çünkü o zaman ilkindeki başarısızlığı nasıl yakalayacaksınız?

Böyle bir şey mi var?

this.http.post('http://localhost/auth/token', creds, { 
    headers: headers 
}) 
.map(res => res.json()) 
.do(
    // validate token 
    // decode token 
) 
.thenDo(
    // get user details 
    this.http.get(url, options) 
    .map(res => res.json()) 
    .do(
     //save user and token in localStorage 
    ) 
) 

cevap

0

i Rxjs do and thenDo işlevi hakkında çok şey bilmiyorum ama bu

this.http.post('http://localhost/auth/token', creds, { 
     headers: headers 
    }) 
    .map(res => { 
     return [{status: res.status , json: res.json()}] 
     }) 
    .subscribe(res=>{ 
     if(res[0].status == 200){ // do you action depends on status code you got assuming 200 for OK response 
      this.validateToken() // Validate your token here in some method named as validateToken 
      this.decodeToken() // decode token here in this method 
      this.getUserDetail() //if everything worked fine call your another get request in another method 
     } 
     }, 
     err => { 
      console.log(err, err.status) //catch your error here 
     }) 

     getUserDetail(){ 
      // make http get request for user detail and saveing into locastroage 
     } 
0

kullanma flatMap her yeni Promise veya gözlemlenebilir dönmek şu zincir operasyonları için iyi bir yoldur gibi evet yapabilirsiniz. Bir Söz Verme veya Gözlemlenebilir bir işlev üzerinde eşleştirmemiz gerektiğinde, çözümlenen verileri yayan bir akış oluşturmak için flatMap'u kullanabiliriz. Burada bir Gözden Geçirilebilir kullanıcı verisi oluşturuyoruz ve sonunda buna abone olabiliriz (yerlilerden tasarruf etmek için).

Doğrulama kodunuzun yalnızca bir Promise veya Observable döndüren bir işlev olduğunu varsadım.

const options = { headers }; 
const user$ = this.http.post('http://localhost/auth/token', creds, options) 
    .map(res => res.json()) 
    .flatMap(validationFunctionThatReturnsAPromise) 
    .flatMap(authResponse => { 
    // get user details 
    return this.http.get(url, options).map(res => res.json()); 
    }); 

user$.subscribe(user => /** do something with the user data **/);