Backbone.React.Component is a wrapper 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.
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.
Include the script on your webpage (or use RequireJS/Browserify)
...
<script type="text/javascript" src="path_to_script/backbone-react-component-min.js"></script>
...
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).
/** @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(function (html) {
console.log(html);
});
// Updating the model
model.set('helloWorld', 'Hi again!');
helloWorld.toHTML(function (html) {
console.log(html);
});
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
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. The usage of the new keyword is optional.
Inspired by Backbone, it inherits a component definition (class) to a new one.
Inspired by Backbone.View, it's a shortcut to this.$el.find method.
Gets the collection from the component's owner.
Gets the model from the component's owner.
Gets the component owner (greatest parent component).
- 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, passes to the callback an HTML string representation of the component. Check React documentation for more information.
- 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).
- Improve models/collections requests error handling
- Any ideas?