Reputation: 5957
Angular 5, NGRX 5
When my app is first loaded, I have an empty List that is in my store. This is a list used by multiple components in my application.
What I would like to do is select the List from the store:
this.terminals$ = this.store$.select(...);
If the list is null, I would like to populate the list using an action/effect:
this.store$.dispatch(new TerminalActions.GetTerminals());
Is it possible to do this in the select?
i.e. call select, if the list is null, call an effect to populate the list?
I'd like to do this in the select so that each component I use this list, I can just call the select, and not have to run an action to populate the list.
Upvotes: 2
Views: 2001
Reputation: 15505
You might not like this answer, but a selector
is and should remain pure (input in, output out, no side effects). Therefore a selector shouldn't dispatch actions.
If you don't want to repeat this logic or dispatch these actions, you can take a look at router guards. See https://toddmotto.com/preloading-ngrx-store-route-guards by Todd Motto for more info. Depending on your implementation this might "freeze" a navigation until data is available in the store.
Upvotes: 5