Skip to content

Improve defining action types #93

@jhollingworth

Description

@jhollingworth

Our current approach to defining action types has a number of drawbacks:

  1. We can't support ES6 function declarations (e.g. { foo() { })
  2. Feedback I've had is that invokable constants is a little odd (I agree)
  3. Action creators become really long (e.g. updateCriteria: SomeConstants.UPDATE_CRITERIA(function (profileId, criteria) { ...)
  4. We mess with function contexts

Function annotations solve this problem perfectly. Unfortunately it won't be in any spec anytime soon. So we need an alternative approach.

The two contenders right now:

Have a type hash

  • Pros
    • Simple, easy to understand, terse
    • Precedent: We already do this with stores
  • Cons
    • Action type is not co-located with action function
    • Cannot support properties
    • Messing with context
var UserActionCreators = Marty.createActionCreators({
  types: {
    addUser: Constants.ADD_USER
  },
  addUser: function(user) {
    var user = UserUtils.createUser(name);
    this.dispatch(user);
    return UserHttpAPI.createUser(user);
  }
})

Invoke constant inside of creator

  • Pros
    • Don't mess with context
    • Don't have to proxy action creator functions
    • Can have multiple action types to a function
  • Cons
    • More complicated, need understand JS well to use this
    • 1 extra line of code
    • Cannot enforce everything has an action type
var UserActionCreators = Marty.createActionCreators({
  addUser: function(name) {
    return Constants.ADD_USER(function(dispatch) {
      var user = UserUtils.createUser(name);
      dispatch(user);
      return UserHttpAPI.createUser(user);
    }, this);
  }
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions