mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Move backbone integration into its own mixin
This commit is contained in:
parent
875782cc0a
commit
c7d2760521
|
|
@ -128,14 +128,34 @@ var TodoFooter = React.createClass({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// An example generic Mixin that you can add to any component that should react to changes in a Backbone component.
|
||||||
|
// The use cases we've identified thus far are for Collections -- since they trigger a change event whenever
|
||||||
|
// any of their constituent items are changed there's no need to reconcile for regular models.
|
||||||
|
// One caveat: this relies on getBackboneModels() to always return the same model instances throughout the
|
||||||
|
// lifecycle of the component. If you're using this mixin correctly (it should be near the top of your
|
||||||
|
// component hierarchy) this should not be an issue.
|
||||||
|
var BackboneMixin = {
|
||||||
|
componentDidMount: function() {
|
||||||
|
// Whenever there may be a change in the Backbone data, trigger a reconcile.
|
||||||
|
this.getBackboneModels().map(function(model) {
|
||||||
|
model.on('add change remove', this.forceUpdate, this);
|
||||||
|
}.bind(this));
|
||||||
|
},
|
||||||
|
componentWillUnmount: function() {
|
||||||
|
// Ensure that we clean up any dangling references when the component is destroyed.
|
||||||
|
this.getBackboneModels().map(function(model) {
|
||||||
|
model.off(null, null, this);
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var TodoApp = React.createClass({
|
var TodoApp = React.createClass({
|
||||||
|
mixins: [BackboneMixin],
|
||||||
getInitialState: function() {
|
getInitialState: function() {
|
||||||
return {editing: null};
|
return {editing: null};
|
||||||
},
|
},
|
||||||
// Here is "the backbone integration." Just tell React whenever there *might* be a change
|
|
||||||
// and we'll reconcile.
|
|
||||||
componentDidMount: function() {
|
componentDidMount: function() {
|
||||||
this.props.todos.on('add change remove', this.forceUpdate, this);
|
// Additional functionality for todomvc: fetch() the collection on init
|
||||||
this.props.todos.fetch();
|
this.props.todos.fetch();
|
||||||
},
|
},
|
||||||
componentDidUpdate: function() {
|
componentDidUpdate: function() {
|
||||||
|
|
@ -145,8 +165,8 @@ var TodoApp = React.createClass({
|
||||||
todo.save();
|
todo.save();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
componentWillUnmount: function() {
|
getBackboneModels: function() {
|
||||||
this.props.todos.off(null, null, this);
|
return [this.props.todos];
|
||||||
},
|
},
|
||||||
handleSubmit: React.autoBind(function() {
|
handleSubmit: React.autoBind(function() {
|
||||||
var val = this.refs.newField.getDOMNode().value.trim();
|
var val = this.refs.newField.getDOMNode().value.trim();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user