Minimalist unidirectional data flow for CRUD
Unicycle minimizes the amount of boilerplate needed to get a Flux flow up and running. You get basic CRUD support for free as data in all stores are treated as arrays.
No constants are used - instead action types are autogenerated from store and
action creator method names (eg. a store named book
and an action creator method named rate
would yield an action type of BOOK_RATE
).
// ./unicycle.js
var Unicycle = require("unicycle");
module.exports = new Unicycle(); // Create a unicycle instance.
// ./stores/BookStore.js
var unicycle = require("../unicycle");
module.exports = unicycle.crudStore("book", function (data) {
return {
title: data.title || "Untitled",
author: data.author || "anonymous",
published: data.published || null;
};
});
// ./actions/BookActions.js
var unicycle = require("../unicycle");
module.exports = unicycle.crudActions("book");
// ./index.js
var React = require("react");
var App = require("./components/App");
var BookStore = require("./stores/BookStore");
var BookActions = require("./actions/BookActions");
var data = require("./data/books.json"); // Get data from somewhere.
BookStore.register(render); // Register a function to be called on changes.
BookActions.read(data); // Read data into store via action.
function render () {
var books = BookStore.getAll();
React.render(<App books={books} />, document.body);
}
This is the main API for unicycle. Simply create a single instance to use throughout your app.
(Object) - A Unicycle instance.
var Unicycle = require("unicycle");
module.exports = new Unicycle();
The Flux dispatcher instance.
Creates an object with action creator methods.
name
(String) - A name that is used to identify the action creators.signatures
(Object) - An object describing the signature of the action creators to be created. The keys will be turned into names of methods and values turned into arguments signature of said methods.
(Object) An object with action creator methods.
var BookActions = unicycle.actions("book", {rate: ["id", "rating"]});
/*
BookActions == {
rate: function (id, rating) { ... }
}
*/
// Later on...
BookActions.rate(book.id, 3);
// The dispatcher would then send the following action to stores:
// {type: "BOOK_RATE", id: "id-of-that-book", rating: 3}
An extension of [unicycle].actions that will automatically include the signatures for basic CRUD actions:
{
create: ["data"],
read: ["data"],
update: ["id", "data"],
delete: ["id"]
}
You can override the included signatures as you like.
See [unicycle].actions. The signatures
argument becomes optional.
(Object) An object with action methods.
Creates a store with handlers for actions.
name
(String) - A name to identify the store.handlers
(Object) - And object with handler functions for reacting to actions. In handlersthis
refers to the Store instance. If you want the store to handle action types other than from action creators of the samename
- simply name the handlers with full action types: uppercasedname + "_" + method
, eg. action creator named"author"
and a method named"wrote"
would be predefined as"AUTHOR_WROTE"
.
(Object) A storefront object.
Creates a store with handlers for actions - but with added handlers for CRUD:
{
create: function (action) {
/* creates new store item using `create` with added `id`, `c_time` and `m_time` properties */
},
read: function (action) {
/* read data into store items */
},
update: function (action) {
/* update existing item via `id` - including autoupdate of `m_time` */
},
delete: function (action) {
/* delete item from store items via `id` */
}
}
name
(String) - A name to identify the store.create
(Function) - Optional. A function to create items in the store. If not supplied raw data is used for item initiation.handlers
(Object) - Optional. See [unicycle].store.
(Object) A storefront object.
Token used to identify the store in Dispatcher.waitFor.
A method that will find an item.
id
Id of the item to find.
(Object) An item from the store.
Returns the items currently in the store.
(Array) The items from the store.
Register functions to be executed on changes.
func
(Function) - The function to be executed once a change has happened.
Unregister a function from being executed on changes.
func
(Function) - The function to be unregistered.
The following describes the internal API that isn't exposed through unicycle
- but may be useful to understand.
name
(String) - A name to identify the store.handlers
(Object) - See [unicycle].store.dispatcher
(Object) - An Flux dispatcher instance.
(Object) - A Store instance.
The token assigned to the store from the dispatcher.
The action handlers attached to the store.
The list of items in the store.
A method to trigger a change event to execute any registered function.