I have a browserify/node kind of application and wanted to use this module to bind backbone collections/models to react components. Unfortunately when I call fetch()
method on collection/model then component not listening for changes on sync
events. The factory approach does not work in that case as I need a component body. I try to set props such as collections using setDefaultProps
. Here is my example file:
"use strict";
var React = require("react"),
Router = require("react-router"),
$ = require("jquery"),
_ = require("underscore"),
Backbone = require("backbone"),
backboneMixin = require("backbone-react-component");
Backbone.$ = $;
var FieldModel = Backbone.Model.extend({
idAttribute: "name"
});
var FieldsCollection = Backbone.Collection.extend({
model: FieldModel,
url: "/fake-api/fields.json",
parse: function (response) {
return response.fields;
}
});
var Fields = React.createClass({
mixins: [backboneMixin],
getDefaultProps: function () {
return {
collection: {
fields: new FieldsCollection()
}
};
},
componentDidMount: function () {
this.getCollection().fields.fetch();
},
createEmptyRow: function () {
console.warn("empty row");
return "";
},
createFieldRow: function (field) {
console.warn(field);
return "";
},
createTable: function () {
console.warn(this.props.fields);
return (
<div className="table-responsive">
<table className="table table-striped table-bordered">
<thead>
<th>Row #</th>
</thead>
<tbody>
{this.props.fields.length === 0 ? this.createEmptyRow() : this.props.fields.map(this.createFieldRow)}
</tbody>
</table>
</div>
);
},
render: function () {
return (
<div>
{this.createTable()}
</div>
);
}
});
module.exports = Fields;