-
Notifications
You must be signed in to change notification settings - Fork 75
Closed
Labels
Description
Our current approach to defining action types has a number of drawbacks:
- We can't support ES6 function declarations (e.g.
{ foo() { }) - Feedback I've had is that invokable constants is a little odd (I agree)
- Action creators become really long (e.g.
updateCriteria: SomeConstants.UPDATE_CRITERIA(function (profileId, criteria) { ...) - 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);
}
})