|
1 | 1 | // Backbone React Component
|
2 | 2 | // ========================
|
3 | 3 | //
|
4 |
| -// Backbone.React.Component v0.9.0 |
| 4 | +// Backbone.React.Component v0.10.0 |
5 | 5 | //
|
6 |
| -// (c) 2014 "Magalhas" José Magalhães <[email protected]> |
| 6 | +// (c) 2014, 2015 "Magalhas" José Magalhães <[email protected]> |
7 | 7 | // Backbone.React.Component can be freely distributed under the MIT license.
|
8 | 8 | //
|
9 | 9 | //
|
|
25 | 25 | // }
|
26 | 26 | // });
|
27 | 27 | // var model = new Backbone.Model({foo: 'bar'});
|
28 |
| -// React.render(<MyComponent model={model} />, document.body); |
| 28 | +// ReactDOM.render(<MyComponent model={model} />, document.body); |
29 | 29 |
|
30 | 30 | (function (root, factory) {
|
31 | 31 | // Universal module definition
|
32 | 32 | if (typeof define === 'function' && define.amd) {
|
33 |
| - define(['react', 'backbone', 'underscore'], factory); |
| 33 | + define(['react', 'react-dom', 'backbone', 'underscore'], factory); |
34 | 34 | } else if (typeof module !== 'undefined' && module.exports) {
|
35 |
| - module.exports = factory(require('react'), require('backbone'), require('underscore')); |
| 35 | + module.exports = factory(require('react'), require('react-dom'), require('backbone'), require('underscore')); |
36 | 36 | } else {
|
37 |
| - factory(root.React, root.Backbone, root._); |
| 37 | + factory(root.React, root.ReactDOM, root.Backbone, root._); |
38 | 38 | }
|
39 |
| -}(this, function (React, Backbone, _) { |
| 39 | +}(this, function (React, ReactDOM, Backbone, _) { |
40 | 40 | 'use strict';
|
41 | 41 | if (!Backbone.React) {
|
42 | 42 | Backbone.React = {};
|
|
70 | 70 | },
|
71 | 71 | // Sets `this.el` and `this.$el` when the component mounts.
|
72 | 72 | componentDidMount: function () {
|
73 |
| - this.setElement(React.findDOMNode(this)); |
| 73 | + this.setElement(ReactDOM.findDOMNode(this)); |
74 | 74 | },
|
75 | 75 | // Sets `this.el` and `this.$el` when the component updates.
|
76 | 76 | componentDidUpdate: function () {
|
77 |
| - this.setElement(React.findDOMNode(this)); |
| 77 | + this.setElement(ReactDOM.findDOMNode(this)); |
78 | 78 | },
|
79 | 79 | // When the component gets the initial state, instance a `Wrapper` to take
|
80 | 80 | // care of models and collections binding with `this.state`.
|
|
134 | 134 | if (this.$el) {
|
135 | 135 | els = this.$el.find.apply(this.$el, arguments);
|
136 | 136 | } else {
|
137 |
| - var el = React.findDOMNode(this); |
| 137 | + var el = ReactDOM.findDOMNode(this); |
138 | 138 | els = el.querySelector.apply(el, arguments);
|
139 | 139 | }
|
140 | 140 |
|
|
283 | 283 | }
|
284 | 284 | this.setState.apply(this, arguments);
|
285 | 285 | },
|
| 286 | + // Get the attributes for the collection or model as array or hash |
| 287 | + getAttributes: function (modelOrCollection){ |
| 288 | + var attrs = []; |
| 289 | + |
| 290 | + // if a collection, get the attributes of each, otherwise return modelOrCollection |
| 291 | + if (modelOrCollection instanceof Backbone.Collection) { |
| 292 | + for (var i = 0; i < modelOrCollection.models.length; i++) { |
| 293 | + attrs.push(modelOrCollection.models[i].attributes); |
| 294 | + } |
| 295 | + return attrs; |
| 296 | + } else { |
| 297 | + return modelOrCollection.attributes |
| 298 | + } |
| 299 | + }, |
286 | 300 | // Sets a model, collection or object into state by delegating to `this.component.setState`.
|
287 | 301 | setState: function (modelOrCollection, key, target, isDeferred) {
|
288 | 302 | var state = {};
|
289 |
| - var newState = modelOrCollection.toJSON ? modelOrCollection.toJSON() : modelOrCollection; |
| 303 | + var newState = this.getAttributes(modelOrCollection); |
290 | 304 |
|
291 | 305 | if (key) {
|
292 | 306 | state[key] = newState;
|
|
0 commit comments