Backbone.React.Component is a wrapper or mixin for React.Component and brings all the power of Facebook's React to Backbone.js.
It works as a bridge between React and Backbone enabling data binding between models, collections and components both on the client and server sides.
It comes in two flavours, a ready to use React mixin or a wrapper class with an extended API.
Table of Contents generated with DocToc
- Dependencies
- How To
- Examples
- Tips
- TO DO
Using Bower
bower install backbone-react-component
Using Npm
npm install backbone-react-component
If you're not using Bower nor Npm download the source from the dist folder or use CDNJS.
The usage of Backbone.React.Component.mixin is similar to Backbone.React.Component, though it relies on React top level API instead of wrapping it.
var MyComponent = React.createClass({
mixins: [Backbone.React.Component.mixin],
render: function () {
return <div>{this.props.foo}</div>;
}
});
var model = new Backbone.Model({foo: 'bar'});
var myComponent = <MyComponent model={model} />;
React.renderComponent(newComponent, document.body);
model.set('test', 'Hello world!');
It follows all the principles behind React.Component, though it binds models and collections to the component's props besides giving you a set of extra methods (extend, toHTML, getModel, getCollection, $, etc). Many of the principles found in the following examples are applied to the mixin version as well.
/** @jsx React.DOM */
var MyComponent = Backbone.React.Component.extend({
render: function () {
return <div>{this.props.test}</div>;
}
});
var model = new Backbone.Model();
var newComponent = new MyComponent({
el: $('body'),
model: model
});
model.set('test', 'Hello world!');
The Component will listen to any model changes, making it automatically refresh using React's algorithm.
newComponent.mount();
var newComponent = new MyComponent({
el: $('body'),
collection: new Backbone.Collection([{helloWorld: 'Hello world!'}])
});
var MyComponent = Backbone.React.Component.extend({
createEntry: function (entry) {
return <div>{entry.helloWorld}</div>;
},
render: function () {
return <div>{this.props.collection.map(this.createEntry)}</div>;
}
});
var newComponent = new MyComponent({
el: $('body'),
model: {
firstModel: new Backbone.Model({helloWorld: 'Hello world!'}),
secondModel: new Backbone.Model({helloWorld: 'Hello world!'})
},
collection: {
firstCollection: new Backbone.Collection([{helloWorld: 'Hello world!'}]),
secondCollection: new Backbone.Collection([{helloWorld: 'Hello world!'}])
}
});
var MyComponent = Backbone.React.Component.extend({
createEntry: function (entry) {
return <div>{entry.helloWorld}</div>;
},
render: function () {
return (
<div>
{this.props.firstModel.helloWorld}
{this.props.secondModel.helloWorld}
{this.props.firstCollection.map(this.createEntry)}
{this.props.secondCollection.map(this.createEntry)}
</div>
);
}
});
var Backbone = require('backbone');
var Component = require('backbone-react-component');
var React = require('react');
var model = new Backbone.Model({
helloWorld: 'Hello world!'
});
var HelloWorld = Component.extend({
render: function () {
return React.DOM.div({}, this.props.helloWorld);
}
});
var helloWorld = new HelloWorld({
model: model
});
helloWorld.toHTML();
// Updating the model
model.set('helloWorld', 'Hi again!');
helloWorld.toHTML();
Backbone.React.Component is nothing more nothing less than a bridge between Backbone and React.
The following diagram illustrates how the data binding is achieved between our models/collections and a React.Component:
Bridge between Backbone and React
The following API is available on both mixin (through Backbone.React.Component.mixin) or Backbone.React.Component (through Backbone.React.Component.extend).
Inspired by Backbone.View, it's a shortcut to this.$el.find method.
Crawls to the owner of the component searching for a collection.
Crawls to the owner of the component searching for a model.
Gets the component owner (greatest parent component).
Besides inheriting all the methods from React.Component and Backbone.Events you can find the following methods:
props is an object and may contain el, model and collection properties. Model and collection properties may be multiple by passing an object as their values.
This is a static method inspired by Backbone, it inherits a component definition (class) to a new one.
- el (DOMElement)
- onRender (Callback) Mounts the component into the DOM.
Unmounts the component. Throws an error if the component doesn't unmount successfully.
Stops component listeners and unmounts the component.
Intended to be used on the server, returns an HTML string representation of the component.
Returns a clone of the component.
- When using the component instead of the mixin remember your root components start listening to model and collection changes when created. To dispose of them call the remove method (mount/unmount no longer starts/stops listeners). If you're using the mixin you don't have to worry because disposal is automatic when the component unmounts.
- Any ideas?