Lightweight data store for React applications using Backbone models/collections
npm install react-backbone-app-store --save
bower install react-backbone-app-store --save
First, initialize an AppStore
and register models.
var AppStore = require('react-backbone-app-store');
var app = new AppStore();
app.registerModel('Users', UsersCollection, '/api/users');
app.registerModel('Items', ItemsCollection, '/api/items');
// ...
var rootProps = {
// default props for root node
};
var rootNode = RootReactComponentType;
var rootParentNode = document.getElementById('root-element');
// Render the application
app.resetData(rootProps, rootNode, rootParentNode);
Register a Backbone model for the app store
@param {String} name The name to use for the model (plural)
@param {Object} collection The Backbone collection for the model
@param {String} endpoint The base URL for CRUD operations on this model
Reset the application data and re-render the top-level of the app
@param {Object} data The root props of the toplevel element
(including app store models, such that
data[modelName] = array of model objects
@param {React} rootNodeType The toplevel React component type
@param {DOM} rootParentNode The DOM element where the app lives
Take in a mapping of model-type to array-of-model objects and reset this.modelHash as a mapping of model-type to Backbone Collection of that model type (this is useful when we want to update/delete/etc. models later)
@param {Object} modelHash Map of model names to arrays of model objects
Re-render the root with (presumably) new model data
Fetch a set of ids from the server and store their models in model hash
@param {String[]} ids Ids of models to fetch
@param {String} modelName The name of the type of model to fetch
@param {Function} cb Optional callback
Get cached model with particular id and type
@param {String} id The id of the desired model
@param {String} modelName The name of the desired model type
@return {Object} The model object or undefined if not yet fetched
Get all the models in a particular collection
@param {String} modelName The name of the desired model type
@return {Object[]} An array of all the model objects
Add a model to the model hash without fetching from server
@param {Object} modelData The model object (must include _id)
@param {String} modelName The name of the model type
Update fields on model of particular type with particular id
@param {Object} updateObject Key-value-pairs specifying fields to update
@param {String} id Id of the model to be updated
@param {String} modelName Name of the type of model being updated
@param {Function} callback Callback once model has been synced with db
Sync a particular model with data in the database
@param {String} id Id of the model to be synced
@param {String} modelName Name of the type of model getting synced
@param {Function} callback Optional callback
Determine whether app store has registered a model with given name
@param {String} modelName The name of the model in question
@return {Boolean} Whether the model has been registered