Parris
Parris

Reputation: 18438

What's the difference between a store and a model?

In flux based applications there is a concept called a store. I've been trying to determine what features a store has and how they differ from models? Does server communication happen in a store? Where does that occur? Are they always singletons?

Upvotes: 3

Views: 1964

Answers (1)

fisherwebdev
fisherwebdev

Reputation: 12690

Stores are domain models, not ORM models.

They manage application state for a logical domain. They can manage state using collections, single values or a combination of both.

But they have a number of specific features that set them apart from normal models:

  1. They have no setters. No one changes the stores from the outside.
  2. The only way data gets into stores is through the callback they register with the dispatcher. They receive every action that goes through the system through this callback. They define which actions they will respond to, ignoring most of them.
  3. The only methods they publicly expose are a set of getters and methods to register/unregister listeners.
  4. When the state they manage changes, they emit a change event. This alerts the view layer that the state of the store has changed, so the views can query for the new data they need, using the getters.

They can call for new data, but when that data is returned it should be in the form of a new action so that all the stores may respond to it. Doing this ensures the resiliency of the application -- you always have all new data throughout the entire system.

Some people prefer to call for new data in the action creators, rather than the stores, which enforces that the new data will originate with an action. This is perfectly acceptable and actually is more common, I believe. But really either style is fine.

Upvotes: 4

Related Questions