You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
After a couple of months working with Redux, I see some patterns in my Effects. In my app, most of the Effects are responsible for:
Making a request to the backend.
Performing actions on success or error - show notification, open/close modal, redirect to the specific page, etc.
Dispatch new action on success or error.
As in the example below:
@Effect()
getUsersRequest$: Observable<Action>=this.actions$.ofType(users.ActionTypes.GET_USERS_REQUEST).map((action: users.GetUsersRequestAction)=>action.payload).switchMap((payload: any)=>{returnthis.usersService.getUsers(payload).do(response=>{// Do something on success, e.g. show notification, open/close modal, redirect page, etc.},error=>{// Do something on error, e.g. show notification, open/close modal, redirect page, etc.}).map(response=>newusers.GetUsersSuccessAction(response.json())).catch(error=>of(newusers.GetUsersErrorAction(catchErrorJson(error))));});
Since it is a common pattern, I start thinking about some way to make it more general and follow the DRY rule. Maybe, I can create a function which takes an object with API endpoint, instructions for success and errors, and use it to build Effect dynamically (instead of creating it manually). For example use the below object to create Effect from the above:
I now that there will be at least one Effect that will be different from the pattern and in this case I need to write it manually but this can work for most of the cases. What do you think about this idea? Do you see any cons of this solution?
The text was updated successfully, but these errors were encountered:
zacol
changed the title
Use object to create Effect dynamically? (discussion)
[Question] Use object to create Effect dynamically?
Jul 3, 2017
Yes, this is possible. We're migrating over to https://github.com/ngrx/platform, so I'm going to close this issue. Please use our gitter channel(s) for support / how-to questions in the future. Thanks!
After a couple of months working with Redux, I see some patterns in my Effects. In my app, most of the Effects are responsible for:
As in the example below:
Since it is a common pattern, I start thinking about some way to make it more general and follow the DRY rule. Maybe, I can create a function which takes an object with API endpoint, instructions for success and errors, and use it to build Effect dynamically (instead of creating it manually). For example use the below object to create Effect from the above:
I now that there will be at least one Effect that will be different from the pattern and in this case I need to write it manually but this can work for most of the cases. What do you think about this idea? Do you see any cons of this solution?
The text was updated successfully, but these errors were encountered: