Biff
Flux Architecture Made Easy
What is Biff?
When writing ReactJS apps, it is enormously helpful to use Facebook's Flux architecture. It truly complements ReactJS' unidirectional data flow model. Facebook's Flux library provides a Dispatcher, and some examples of how to write Actions and Stores. However, there are no helpers for Action & Store creation, and Stores require 3rd part eventing.
Biff is a library that provides all 3 components of Flux architecture, using Facebook's Dispatcher, and providing factories for Actions & Stores.
Demo
Check out this JSFiddle Demo to see how Biff can work for you:
Getting Started
The first step to using Biff is to create a new instance of Biff.
Standalone
var biff = ;
Modular
var Biff = ;moduleexports = ;
Each instance of Biff has its own Dispatcher instance created and attached.
In fact, all created Actions & Stores are also stored on the Biff object as actions
and stores
respectively.
biffdispatcher // Dispatcher instancebiffactions // Array of actionsbiffstores // Array of stores
Stores
Biff has a createStore helper method that creates an instance of a Store. Store instances have been merged with EventEmitter and come with emitChange, addChangeListener and removeChangeListener methods built in.
When a store is created, its methods parameter specified what public methods should be added to the Store object. Every store is automatically registered with the Dispatcher and the dispatcherID
is stored on the Store object itself, for use in waitFor
methods.
Creating a store with Biff looks like this:
// Require the Biff instance you createdvar biff = ; // Internal data objectvar _todos = ; { _todos;} var TodoStore = biff;
Use Dispatcher.waitFor
if you need to ensure handlers from other stores run first.
var biff = ;var Dispatcher = Biffdispatcher;var OtherStore = ; var _todos = ; { _todos;} ... case 'ADD_TODO': Dispatcher; var someValue = OtherStore; ; break; ...
Stores are also created a with a ReactJS component mixin that adds and removes store listeners that call an storeDidChange component method.
Adding Store eventing to your component is as easy as:
var TodoStore = ; var TodoApp = React
This mixin also adds listeners that call a storeError component method, so that if you call this.emitError('Error Messaging')
in your store, you can respond and handle this in your components:
var TodoStore = ; var TodoApp = React
A simple example of how this works can be seen here:
Stores in Biff also have helpers for managing the state of the store's data. Each Biff instance has _pending
and _errors
properties. These are exposed via getters and setters. These methods are:
- getPending() - Returns store pending state
- getErrors() - Returns store errors array
- _setPending(arg) - Sets store pending state
- _setError(arg) - Adds an error to the store errors array
- _clearErrors() - Clears store errors array
Below, see an example of how they can be used:
// In Your Store var TodoStore = biff; // In your component { return errors: TodoStore pending: TodoStore todos: TodoStore } var TodoApp = React
Actions
Biff's createActions method creates an Action Creator object with the supplied singleton object. The methods of the supplied object are given an instance of the Biff instance's dispatcher object so that you can make dispatch calls from them. It is available via this.dispatch
in the interior of your methods.
Adding actions to your app looks like this:
var biff = ; var TodoActions = biff;
Async In Biff
Check out the example below to show you can handle async in Biff:
API
Biff
var Biff = ; moduleexports = ;
createStore
/* * @param {object} methods - Public methods for Store instance * @param {function} callback - Callback method for Dispatcher dispatches * @return {object} - Returns instance of Store */
createActions
/** * @param * @constructor */