Reputation: 3275
I have observable data in my angular2 app. The data has 2 properties
{isFetching: false, user: {userID: 1, firstName: "john", lastName: "public"} }.
When the data is getting retrieved from server isFetching flag is true and user object is null. In one of my component I need to get some other data by userID that is on the observable object.Initially when the observable is subscribed user object is null. In my code I am having to check if the user is null before performing some other action. Below code works, but I think there has to be better way than having if condition inside subscribe block.
this.store.select<UserProfileState>('user')
.subscribe(user => {
if (!user.isFetching) {
this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID));
}
});
Any help is appreciated.
Upvotes: 0
Views: 5448
Reputation: 23803
As you're working with an Observable, you can use filter :
this.store.select<UserProfileState>('user')
.filter(user => !user.isFetching)
.subscribe(user => {
this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID));
});
Upvotes: 3