我正在尝试使用activities: Activity[]
状态过滤ngrx selectedProject
状态。 selectedProject
的密钥用于获取可观察的[{$key, true}, ...]
。如何过滤activities: Activity[]
州?对于曾经使用过rxjs的人来说,这可能是一个非常简单的问题。
let obs: Observable<Project> = this.appstore.select('selectedProject');
let actskeys: FirebaseListObservable<any[]>;
obs.subscribe(val =>
{
id = val.$key;
actskeys = this.db.list(`/projectActs/${id}`);// This returns [{$key: true},{$key: true},...]
this.appstore.select('activities'); // I want to filter this store using the actkeys, $key.
}
);
我尝试非工作代码
getActsByCourseId(): Observable<Activity[]>{
let id: string;
let actskeys: FirebaseListObservable<any[]>;
let obs: Observable<Project> = this.appstore.select('selectedProject');
let acts = this.appstore.select('activities')
let filteredActs: Activity[] = [];
obs.subscribe(val =>
{
id = val.$key;
actskeys = this.db.list(`/projectActs/${id}`);
actskeys.subscribe(val => {val.forEach(bb =>
acts
.filter((res: Activity[]) => res && res.length > 0 )
.map((res: Activity[]) => res.filter(val => val.$key === bb.$key))
.subscribe(res => console.log(res))
);
this.appstore.dispatch({ type: LOAD_Activities, payload: filteredActs});
}
)
}
);
return this.appstore.select('activities');
}
更新代码:尝试过combineLatest
let selectedProject$ = this.appstore.select<Project>('selectedProject');
let activities$ = this.appstore.select<Activity[]>('activities');
let actskeys$: FirebaseListObservable<any[]>;
let filteredActs: Observable<any[]> ;
selectedProject$
//.withLatestFrom(actskeys$, activities$)
.map((selectedProject) => {
id = selectedProject.$key;
return actskeys$ = this.db.list(`/projectActs/${id}`);
// filter here your activities ...
})
.subscribe(res => {
filteredActs = this.getProjectActs(res);
});
filteredActs.subscribe(
res => {
console.log(res);
// this.appstore.dispatch({ type: LOAD_Activities,
payload: res});
}
);
return this.appstore.select('activities');
}
getProjectActs(actskeys$:FirebaseListObservable<any[]>):
Observable<any[]> {
let activities$ = this.appstore.select<Activity[]>('activities');
return Observable.combineLatest(activities$, actskeys$, (acts, keys) =>
{
return keys.map(key => {
acts.filter(act => act.$key === key.$key);
})
})
};
我从控制台收到[undefined, undefined]
。
答案 0 :(得分:0)
不完全确定你在做什么,但你可以尝试一下:
let selectedProject$ = this.appstore.select<Project>('selectedProject');
let activities$ = this.appstore.select<Activities>('activities');
let actskeys$: FirebaseListObservable<any[]>;
selectedProject$
.withLatestFrom(actskeys$, activities$)
.map([selectedProject, actskeys, activities] => {
id = selectedProject.$key;
actskeys = this.db.list(`/projectActs/${id}`);
// filter here your activities ...
})
.subscribe();