-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReactMeteor.js
More file actions
176 lines (146 loc) · 4.43 KB
/
ReactMeteor.js
File metadata and controls
176 lines (146 loc) · 4.43 KB
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
var ReactMeteorMixin = {
componentWillMount: function() {
var self = this;
self._meteorStateDep = new Tracker.Dependency();
self._meteorFirstRun = true;
if (Meteor.isClient) {
Tracker.autorun(function(computation) {
self._meteorComputation = computation;
self._meteorStateDep.depend();
if (self.startMeteorSubscriptions) {
// Calling this method in a Tracker.autorun callback will ensure
// that the subscriptions are canceled when the computation stops.
self.startMeteorSubscriptions();
}
enqueueMeteorStateUpdate(self);
});
} else {
enqueueMeteorStateUpdate(self);
}
},
componentWillUpdate: function(nextProps, nextState) {
if (this._meteorCalledSetState) {
// If this component update was triggered by the ReactMeteor.Mixin,
// then we do not want to trigger the change event again, because
// that would lead to an infinite update loop.
this._meteorCalledSetState = false;
return;
}
if (this._meteorStateDep) {
this._meteorStateDep.changed();
}
},
componentWillUnmount: function() {
if (this._meteorComputation) {
this._meteorComputation.stop();
this._meteorComputation = null;
}
}
};
function enqueueMeteorStateUpdate(component) {
var partialState =
component.getMeteorState &&
component.getMeteorState();
if (! partialState) {
// The getMeteorState method can return a falsy value to avoid
// triggering a state update.
return;
}
if (component._meteorFirstRun) {
// If it's the first time we've called enqueueMeteorStateUpdate since
// the component was mounted, set the state synchronously.
component._meteorFirstRun = false;
component._meteorCalledSetState = true;
component.setState(partialState);
return;
}
Tracker.afterFlush(function() {
component._meteorCalledSetState = true;
component.setState(partialState);
});
}
// Like React.render, but it replaces targetNode, and works even if
// targetNode.parentNode has children other than targetNode.
function renderInPlaceOfNode(reactElement, targetNode) {
var container = targetNode.parentNode;
var prevSibs = [];
var nextSibs = [];
var sibs = prevSibs;
var child = container.firstChild;
while (child) {
if (child === targetNode) {
sibs = nextSibs;
} else {
sibs.push(child);
}
var next = child.nextSibling;
container.removeChild(child);
child = next;
}
var result = React.render(reactElement, container);
var rendered = container.firstChild;
if (prevSibs.length > 0) {
prevSibs.forEach(function(sib) {
container.insertBefore(sib, rendered);
});
}
if (nextSibs.length > 0) {
nextSibs.forEach(function(sib) {
container.appendChild(sib);
});
}
return result;
}
function unmountComponent(reactComponent) {
var rootNode = React.findDOMNode(reactComponent);
var container = rootNode && rootNode.parentNode;
if (container) {
var siblings = [];
var sibling = container.firstChild;
while (sibling) {
var next = sibling.nextSibling;
if (sibling !== rootNode) {
siblings.push(sibling);
container.removeChild(sibling);
}
sibling = next;
}
React.unmountComponentAtNode(container);
siblings.forEach(function (sib) {
container.appendChild(sib);
});
}
}
ReactMeteor = {
Mixin: ReactMeteorMixin,
// So you don't have to mix in ReactMeteor.Mixin explicitly.
createClass: function createClass(spec) {
spec.mixins = spec.mixins || [];
spec.mixins.push(ReactMeteorMixin);
var Cls = React.createClass(spec);
if (Meteor.isClient &&
typeof Template === "function" &&
typeof spec.templateName === "string") {
var template = new Template(
spec.templateName,
function() {
// A placeholder HTML element that will serve as the mounting
// point for the React component. May have siblings!
return new HTML.SPAN;
}
);
template.onRendered(function() {
this._reactComponent = renderInPlaceOfNode(
// Equivalent to <Cls {...this.data} />:
React.createElement(Cls, this.data || {}),
this.find("span")
);
});
template.onDestroyed(function() {
unmountComponent(this._reactComponent);
});
Template[spec.templateName] = template;
}
return Cls;
}
};