2017-04-14 4 views
0

필터 함수를 반환하려고했으나 반환 값이 콜백과 함께 작동하지 않는 것 같습니다. 여기서 this.store.let(getIsPersonalized$)은 관찰 가능한 방출 부울 값이고 this.store.let(getPlayerSearchResults$)은 비디오 클래스의 관찰 가능한 방출 객체입니다. 동기식으로 실행하려면 어떻게해야합니까? 저장소에서 수신 한 관측 값을 수정할 수 없다는 가정하에 비동기식 콜백을 피할 수 있습니까? 당신의 구독 isPersonalized$에 분기를 얻을 수 있다면관찰자 내에서 돌아 오는 방법은 무엇입니까?

isPersonalized$ = this.store.let(getIsPersonalized$); 
videos$ = this.store.let(getPlayerSearchResults$)      
        .map((vids) => this.myFilter(vids)); 

myFilter(vids) { 
    this.isPersonalized$.subscribe((x){ 
     if(x){ 
     return this.fileterX(vids);//Return from here 
     } 
     else { 
     return this.filterY(vids);//Or Return from here 
     } 
    }); 
} 

fileterX(vids) { 
    return vids.filter((vid) => vids.views>100;); 
} 

fileterY(vids) { 
    return vids.filter((vid) => vids.views<20;); 
} 

답변

0

내가 이런 식으로 일을 가지고, 당신은 모든 myFilter (VIDS)가 필요하지 않습니다. 다음은 업데이트 된 코드입니다.

this.store.let(getIsPersonalized$); 
    videos$: Observable<any>; 

    ngOnInit() { 
    this.isPersonalized$.subscribe((x) => { 
     if (x) { 
     this.videos$ = this.store.let(getPlayerSearchResults$) 
         .map((vids) => this. fileterX(vids)); 
     } else { 
     this.videos$ = this.store.let(getPlayerSearchResults$) 
         .map((vids) => this. fileterY(vids)); 
     } 
    }); 
    } 

    fileterX(vids) { 
    return vids.filter((vid) => vids.views>100;); 
    } 

    fileterY(vids) { 
    return vids.filter((vid) => vids.views<20;); 
    } 
0

그것은 당신이, 내가 withLatestFrom을 통해 (예 그렇게하려는지도 함수 내 isPersonalized$의 최신 가치를 평가하고자 다음과 같습니다 첫 번째는 true/false 매 5 초를 전환, 두 번째는 증가마다 1 초를 방출) :

const isPersonalized$ = Rx.Observable.interval(5000) 
    .map(value => value % 2 === 0); 
const getPlayerSearchResults$ = Rx.Observable.interval(1000) 
    .withLatestFrom(isPersonalized$) 
    .map(bothValues => { 
    const searchResult = bothValues[0]; 
    const isPersonalized = bothValues[1]; 
    ... 
    });