Code Monkey home page Code Monkey logo

Comments (3)

SethDavenport avatar SethDavenport commented on July 29, 2024

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.

SethDavenport avatar SethDavenport commented on July 29, 2024

There are a couple of gotchas. The session action creators now hang off a class instead of being bare functions, because:

  1. I need a class to attach @Injectable() to
  2. 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.

SethDavenport avatar SethDavenport commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.