goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

Add finer control for store state with events #653

Open noahmonster opened 8 years ago

noahmonster commented 8 years ago

Hi,

I currently have a number of stores which contain state across functional areas of my application. For example, I have one store to deal with all state regarding users. I maintain the state of the currently selected user, the list of all users, any filter that a user has applied, etc...

constructor () {
    ...
    this.state = {
        userList: [],
        currentUser: null,
        userFilter: '',
        error: 0,
    }
}

Using the store in this way saves a good amount of boilerplate code. I don't have to have a separate store for each one of the variables in the UserStore's state. I can also reuse functions in the UserStore for each of these items.

The downside to this approach is that the UserStore's change events are overly generic. I may update just one attribute of the state in a given function...

userLoaded (userData) {
    this.setState({currentUser: userData});
}

But then the change event would fire callbacks on components that don't necessarily care about the currentUser. They may be waiting for an update of the userFilter or errors.

Would it be possible to add some type of extra change events so that I could fire events specific to particular pieces of the state? In this case, I would want to trigger a "UserLoaded" change event.

Or should I just separate my state into separate stores? Perhaps there is another way?