2015-10-22 33 views
0

我仍然試圖圍繞使用RxJS和React Native。我正在建立一個ID爲TextInput,密碼TextInput和登錄按鈕的登錄屏幕,他們都有一個通過jsx連接到它們的單獨回調處理程序(因此我無法使用RxJS的'fromEvent'API創建一個可觀察對象)。在觸發器可觀察對象上執行一些可觀察對象的操作 - RxJS

在componentWillMount中,我創建了id文本更改,密碼文本更改和click處理程序的主題。

//In id text change handler 
this.idStream.onNext(newId); 

//In password text change handler 
this.passwordStream.onNext(newPassword); 

//In click handler 
this.buttonClickStream.onNext(null); //null since value is not important 

我只想要一個API請求觸發時,按鈕ID和密碼不是一個空字符串,並單擊該按鈕。

在componentWillMount我試圖

var subscription = this.buttonClickStream 
         .combineLatest(this.idStream, this.passwordStream, function(click, id, password) { 
    return { 
     id: id, 
     password: password 
    }; 
}) 
.filter(credentials => {return credentials.id.length > 0 && 
           credentials.password.length > 0;} 
.subscribe(function(credentials) { 
    console.log('fire API request with credentials'); 
}); 

但是這個問題,因爲我使用combineLatest,API被解僱不只是我每次更改ID或密碼的時間(點擊按鈕也只要他們通過過濾器)。我錯過了什麼?

謝謝!

回答

1

我認爲你可以使用Rx.withLatestFrom而不是combineLatest。它只有在源觀測值觸發並將所傳遞的觀測值的最後發射值作爲參數時纔會觸發。

參考http://rxmarbles.com/#withLatestFrom

+0

那就是那個!謝謝! – coldbuffet

+0

就像上面的例子中的後續問題,單獨處理訂閱就足夠了嗎?或者我也應該'onComplete'我創建的每個主題? – coldbuffet

+0

不確定,也許你可以提高對象與一般觀察對象的一生作爲另一個問題。 – user3743222