-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-mixin.js
181 lines (150 loc) · 5.94 KB
/
react-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function(root, factory) {
if(typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'backbone-rel', 'react'], function(_, Backbone, React) {
return factory(root, _, Backbone, React);
});
} else if(typeof exports !== 'undefined') {
// for Node.js or CommonJS
var _ = require('underscore'),
Backbone = require('backbone-rel'),
React = require('react');
module.exports = factory(root, _, Backbone, React);
} else {
// as a browser global
root.Backbone.ReactMixin = factory(root, root._, root.Backbone, root.React);
}
}(this, function(root, _, Backbone) {
/**
* Performs equality by iterating through keys on an object and returning
* false when any key has values which are not strictly equal between
* objA and objB. Returns true when the values of all keys are strictly equal.
*
* @return {boolean}
*/
function shallowEqual(objA, objB) {
if (objA === objB) {
return true;
}
var key;
// Test for A's keys different from B.
for (key in objA) {
if (objA.hasOwnProperty(key) &&
(!objB.hasOwnProperty(key) || objA[key] !== objB[key])) {
return false;
}
}
// Test for B'a keys missing from A.
for (key in objB) {
if (objB.hasOwnProperty(key) && !objA.hasOwnProperty(key)) {
return false;
}
}
return true;
}
var mixin = {
_isBackboneBound: true,
shouldComponentUpdate: function(nextProps, nextState, nextContext) {
if(this.shouldComponentUpdateOverride) {
return this.shouldComponentUpdateOverride.apply(null, arguments);
}
return this._needsUpdate ||
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState) ||
!shallowEqual(this.context, nextContext);
},
componentWillUpdate: function() {
if(this._needsUpdate) {
delete this._needsUpdate;
}
},
componentDidMount: function() {
this.initBackboneProps(this.props, true);
},
componentWillReceiveProps: function(nextProps) {
this.initBackboneProps(nextProps);
},
initBackboneProps: function(props, initial) {
var key,
prop,
keysForBackboneProps = [];
// listen to Backbone models and collections in the props
for(key in props) {
prop = props[key];
if(prop instanceof Backbone.Model || prop instanceof Backbone.Collection) {
keysForBackboneProps.push(key);
this.reactTo(prop, !initial && key);
}
}
// stop listening to previous props that are not present anymore
for(key in this.props) {
if(_.includes(keysForBackboneProps, key)) return;
prop = this.props[key];
if(prop instanceof Backbone.Model || prop instanceof Backbone.Collection) {
this.stopReacting(prop);
}
}
},
componentWillUnmount: function() {
// stop listening to all props
for(key in this.props) {
prop = this.props[key];
if(prop instanceof Backbone.Model || prop instanceof Backbone.Collection) {
this.stopReacting(prop);
}
}
},
/**
* Starts listening to the 'deepchange' event of the passed model or collection in the component's props
* to keep the component updated
*/
reactTo: function(modelOrCollection, key) {
if(key && this.props[key]) {
if(this.props[key] === modelOrCollection) {
return;
} else {
this.stopReacting(modelOrCollection);
}
}
modelOrCollection.on("deepchange", this._handleDeepChange, this);
modelOrCollection.on("deepchange_propagated", this._handleDeepChangePropagated, this);
modelOrCollection.on("forceUpdate", this._handleForceUpdate, this);
},
stopReacting: function(modelOrCollection) {
modelOrCollection.off("deepchange", this._handleDeepChange, this);
modelOrCollection.off("deepchange_propagated", this._handleDeepChangePropagated, this);
modelOrCollection.off("forceUpdate", this._handleForceUpdate, this);
},
_handleDeepChange: function() {
if(!this.isMounted()) return;
this._needsUpdate = true;
},
_handleDeepChangePropagated: function () {
if(!this.isMounted() || this.hasBackboneBoundOwner()) return;
this.forceUpdate();
},
_handleForceUpdate: function () {
if(!this.isMounted()) return;
this.forceUpdate();
},
hasBackboneBoundOwner: function() {
var owner = this._reactInternalInstance._currentElement._owner;
return owner && owner._instance._isBackboneBound;
},
/**
* Returns a ReactLink for updating the specified attr of the passed model (which should be in the component's props)
* realizing a two-way data binding to Backbone models.
*/
bindTo: function(model, key, getTransformFn, setTransformFn){
getTransformFn = getTransformFn || _.identity;
setTransformFn = setTransformFn || _.identity;
return {
value: getTransformFn(model.get(key)),
requestChange: function(value){
model.set(key, setTransformFn(value));
}.bind(this)
};
}
};
return mixin;
}));