Comments (3)
Messing around, this is the approach I came up with from the angular2 starter:
import { Injectable } from 'angular2/core';
import { LoginService } from '../services/login';
import {
LOGIN_USER_PENDING,
LOGIN_USER_SUCCESS,
LOGIN_USER_ERROR,
LOGOUT_USER
} from '../constants';
@Injectable()
export class SessionActions {
constructor(private loginService: LoginService) {}
loginUser = (credentials) => {
return (dispatch, getState) => {
const username = credentials.username;
const password = credentials.password;
return dispatch({
types: [
LOGIN_USER_PENDING,
LOGIN_USER_SUCCESS,
LOGIN_USER_ERROR,
],
payload: {
promise: this.loginService.login(username, password)
},
});
};
}
logoutUser = () => {
return {
type: LOGOUT_USER,
};
}
}
And then to use it:
@Component({
// etc.
})
export class RioSampleApp {
private unsubscribe: Function;
private isLoggedIn: Boolean;
private session: any;
private selector: Subscription;
constructor(
private ngRedux: NgRedux<IAppState>,
private applicationRef: ApplicationRef,
private sessionActions: SessionActions) {
}
ngOnInit() {
// Etc.
this.ngRedux.mapDispatchToTarget(this.mapDispatchToThis)(this);
}
mapDispatchToThis = (dispatch) => {
const { loginUser, logoutUser } = this.sessionActions;
return {
login: (credentials) => dispatch(loginUser(credentials)),
logout: () => dispatch(logoutUser())
};
};
};
from store.
There are a couple of gotchas. The session action creators now hang off a class instead of being bare functions, because:
- I need a class to attach
@Injectable()
to - I need a class to inject
LoginService
into
However this makes constructs like the following run afoul of this
semantics.
mapDispatchToTarget({
fooAction: this.actionService.foo,
barAction: this.actionService.bar
})(this);
The cleanest thing I could come up with was to make my actionCreator service's methods all arrow function props instead of regular methods.
Alternately I thought about implementing an @autobind
decorator but that seemed like overkill.
I can document the above as a pattern at least.
@e-schultz thoughts?
from store.
If you're OK with this approach, I can commit it to rangle/angular2-redux-starter#105. Unfortunately I made the mistake of rebasing that branch though...
from store.
Related Issues (20)
- One More Time, Error: Actions must be plain objects. Use custom middleware for async actions
- Extend peer dependencies to Angular 7 HOT 2
- "NullInjectorError: No provider for NgZone" after upgrade to Angular 6 and angular-redux/store 9 HOT 2
- `window.devToolsExtension` is deprecated
- Storybook stories with @select HOT 2
- Metadata collected contains an error that will be reported at runtime: Lambda not supported.
- Request for articles/epics.md HOT 2
- Did you forget to configure your store? when running from a dist directory
- Select fires on unrelated store changes.
- Using @select decorator with @Input HOT 1
- Compiler Error when implementing Redux-Thunk Action
- @select decorator error: property has no initializer and is not definitely assigned in the constructor.
- Sharing common store between multiple modules ?
- Support for Angular 8? HOT 5
- installing angular-redux/store on angular 8.0.0 causes warning on missing peer dependencies (@angular/core@^7.0.0) HOT 2
- NgRedux<RootState> HOT 6
- Dispatch an action outside of ngZone HOT 1
- not understanding how to test subscription side effects HOT 1
- An unexpected error occurred: "https://registry.yarnpkg.com/@angular-redux%2fstore: read ETIMEDOUT" HOT 1
- Angular Version 12 is not supporting. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store.