goatslacker / alt

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

Should not Actions have Stores?? #592

Closed bsdo64 closed 8 years ago

bsdo64 commented 8 years ago

I'm using 18.1 now. When I declare import in PostActions, it works fine

import alt from '../alt';
import Api from '../Utils/ApiClient';

class PostActions {
  getPostsByClub(params) {
    return (dispatch) => {
      Api.getPostsByClub(params, function (err, data) {
        if (err) {
          this.redirectToNotFound();
        }
        dispatch(data);
      }.bind(this));
    };
  }
}

but, declare some stores in the action,

import alt from '../alt';
import Api from '../Utils/ApiClient';
import UserStore from '../Stores/UserStore';
import PostStore from '../Stores/PostStore';

class PostActions {
  getPostsByClub(params) {
    return (dispatch) => {
      Api.getPostsByClub(params, function (err, data) {
        if (err) {
          this.redirectToNotFound();
        }
        dispatch(data);
      }.bind(this));
    };
  }
}

Action is undefined. So All the binding is fail. Why this is happend, and is there any way to use other store's data?

jdlehman commented 8 years ago

The problem you are encountering appears to be due to a circular dependency. Your stores import your actions, but then you have your actions importing your stores.

Ways to get around this would be to have higher order stores (or simply combining the data that needs to be shared into a single store). Keep in mind that Facebook recommends that a Flux store does not manage the state of a traditional MVC "model", but the state of a particular domain in the application, which would likely contain multiple models.

Lastly, if you still want to keep these stores separate, you can just send along the data when you dispatch your actions. Remember, your views can subscribe to store data, so you can subscribe to this data and pass it along when a user event triggers an action in that particular view.