2017-07-22 14 views
0

のは、私は以下のクラスを持っていると仮定しましょう:カスタムクラスでRxJSを使用するには?

また
class XHRUpload { 
    constructor() { 
     // Some AJAX code here and listening on progress event, 
     // and then pass the progress info out of this class. 
    } 
} 

私は、被監視オブジェクトを持っている:

const observable = Rx.Observable.create((obs) => { 
    obs.next(/* progress info here */) 
}) 

しかし、私はクラスでこの観察可能なオブジェクトを使用する必要がありますどのようにクラスの外にそれを使用することができるようにします?私が今できることはこれが何であるかを

唯一のもの:

const observable = Rx.Observable.create((obs) => { 
    new XHRUpload(obs) 

    // Then in the class... 
    obs.next(/* progress info here */) 
}) 

// And then... 
observable.subscribe({ 
    next: value => {/* Progress data... */}, 
    error: err => {/* Something bad has happened! */}, 
    complete:() => {/* Transfer has been completed! */} 
}) 

しかし、それは悪い習慣のように思えるが、そうではありませんか?

答えて

0

最後に私はそれを理解しました!

const observable = new Rx.Subject() 

observable.subscribe({ 
    next: value => {/* Progress data... */}, 
    error: err => {/* Something bad has happened! */}, 
    complete:() => {/* Transfer has been completed! */} 
}) 

const obj = new XHRUpload(observable) 

とクラスは次のようになります。

class XHRUpload { 
    constructor(observable) { 
     // Some great code here... 

     observable.next(/* some progress data */) 
    } 
} 
関連する問題