Last active
August 29, 2015 14:27
-
-
Save tutuca/4f77a6d1b3d08e7a1997 to your computer and use it in GitHub Desktop.
A mixin for ReactJS that looks for `this.props.collection` or `this.props.model` listens for all Backbone events to update the React Component.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
], function() { | |
return { | |
componentDidMount: function() { | |
this._boundForceUpdate = this.forceUpdate.bind(this, null); | |
this.getBackboneObject().on("all", this._boundForceUpdate, this); | |
}, | |
componentWillUnmount: function() { | |
this.getBackboneObject().off("all", this._boundForceUpdate); | |
}, | |
getBackboneObject: function() { | |
return this.props.collection || this.props.model; | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
define([ | |
"Backbone", | |
"React", | |
"ListComponent" | |
], function(React, ListComponent) { | |
var ListItem = Backbone.Model.extend({}); | |
var ListItemCollection = Backbone.Collection.extend({ | |
model: ListItem | |
}); | |
var listItemCollection = new ListItemCollection(); | |
React.renderComponent( | |
<ListComponent collection={listItemCollection} />, | |
document.body | |
); | |
listItemCollection.set([ | |
new ListItem(), | |
new ListItem(), | |
new ListItem() | |
]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
define([ | |
"React", | |
"BackboneMixin" | |
], function(React, BackboneMixin) { | |
return React.createClass({ | |
mixins: [BackboneMixin], | |
render: function() { | |
var listItems = this.props.collection.map(function(model) { | |
return (<li key={model.cid}>{model.cid}</li>); | |
}); | |
return ( | |
<ul> | |
{listItems} | |
</ul> | |
); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment