I'm pretty new to RxJS (using 5.1.1) and trying to understand it in my Angular 2 applciation using angular-redux/store.
I've got a store set up and working to get different parts of my state in my application.
Now, I'm trying to get AppParts that have the loaded property set to true:
my state:
export interface IAppState extends IRootState{
theme: Theme;
config: IConfig;
mainSubTitle: string;
appParts: AppPart[];
}
the select:
@select(state => state.app.appParts) appParts$: Observable<AppPart>;
now, I'm trying to get a filtered array from this observable:
loadedAppParts = [];
ngOnInit() {
this.appParts$.filter(a => a.loaded).subscribe(a => { console.log(a); this.loadedAppParts.push(a); });
}
However, this returns an empty array. I'd like to be able to use the async pipe to get the 'loadedAppParts' if possible too, so I've also tried doing the following:
loadedAppParts: Observable<AppPart> = new Observable<AppPart>();
ngOnInit() {
this.loadedAppParts = this.appParts$.filter(a => a.loaded);
}
So, how can I get a filtered array or observable from my Observable state?
Forgot to add my Rootstate:
export interface IRootState { };
and my initial state:
export const INITIAL_STATE: IAppState = {
theme: THEMES.LightGreyBlue,
config: <IConfig>{
data: {
apiUrl: ''
},
general: {
appName: 'Default name',
appShortName: 'default'
}
},
mainSubTitle: 'Default',
appParts: [new AppPart('core', true)]
};
And the template part that displays the JSON to debug (for the array example):
{{ appParts$ | async | json }} {{ loadedAppParts | json }}
When using the Observable:
{{ appParts$ | async | json }} {{ loadedAppParts | async | json }}
This returns: [ { "name": "core", "loaded": true } ] null
appParts$is emitting an array ofAppPartobjects so correct type would beObservable<AppPart[]>. If you try to flatten the emissions withthis.loadedAppParts = this.appParts$.mergeAll().filter(a => a.loaded);does it help?