Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do Action objects serve any real purpose ? #11

Closed
lorefnon opened this issue May 22, 2014 · 2 comments
Closed

Do Action objects serve any real purpose ? #11

lorefnon opened this issue May 22, 2014 · 2 comments

Comments

@lorefnon
Copy link

Action objects like the following:

var actions = {
  addUrl: function(url, index) {
    this.dispatch("ADD_URL", {url: url, index: index});
  }
};

seem like a superfluous layer of indirection that provides no real advantage.

Is there any advantage to having the action objects at all rather than just making a singleton dispatcher available to the views. After all, all that the actions seem to do is translate method calls to dispatch calls, passing on their payload. Why couldn't the views make dispatch calls directly ?

@BinaryMuse
Copy link
Owner

Hey, @lorefnon,

Thanks for the issue—this has come up a couple times, and I should probably include it in the docs. I actually had the same thought when I first started experimenting with flux; they definitely are another layer of indirection, though I'm now not so sure they're superfluous.

You actually can dispatch actions directly if you want via getFlux().dispatcher.dispatch, but that couples the component to:

  • the dispatcher implementation
  • the action type (i.e. the string itself)
  • the format of the payload

In a larger application, the extra indirection will make changes to the action/data layer easier without having to change your application's components. It also allows changes to the Fluxxor dispatcher without needing to change either (1) your components (because they don't know about the dispatcher at all) or (2) your action methods (because this.dispatch is bound to an internal object that handles actually doing the dispatch for you).

Related to the first point above, the actions also give you semantically meaningful method names: flux.actions.addUrl(url) instead of flux.dispatcher.dispatch(Actions.ADD_URL, {url: url}).

See also Creating Semantic Actions on the React site:

As you can see, we really would not need to have the helpers AppDispatcher.handleViewAction() or TodoActions.create(). We could, in theory, call AppDispatcher.dispatch() directly and provide a payload. But as our application grows, having these helpers keeps the code clean and semantic. It's just a lot cleaner to write TodoActions.destroy(id) instead of writing a whole lot of things that our TodoItem shouldn't have to know about.

Hope that helps; this is a really interesting topic to me, so feel free to keep thinking about/discussing it!

@lorefnon
Copy link
Author

Thanks for the reply, @BinaryMuse . I see where you are coming from, thank you for taking the time to elaborate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants