-
Notifications
You must be signed in to change notification settings - Fork 2
/
MarionetteComponent.jsx
80 lines (68 loc) · 2.18 KB
/
MarionetteComponent.jsx
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
/**
* Developer: Stepan Burguchev
* Date: 2/5/2017
* Copyright: 2015-2017 ApprovalMax
* All Rights Reserved
*
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ApprovalMax
* The copyright notice above does not evidence any
* actual or intended publication of such source code.
*/
import Marionette from 'backbone.marionette';
import React from 'react';
import PropTypes from 'prop-types';
class MarionetteComponent extends React.Component {
constructor(props) {
super(props);
this._el = null;
this._view = null;
this._hostRegion = null;
this._regionManager = null;
}
componentDidMount() {
this._regionManager = new Marionette.RegionManager();
this._hostRegion = this._regionManager.addRegion('hostRegion', {
el: this._el
});
this._rebuildView();
}
componentWillReceiveProps(nextProps) {
if (this.props.view !== nextProps.view) {
throw new Error(
'[MarionetteComponent] error: `props.view` cannot be changed after the initial render.'
);
}
}
componentDidUpdate(prevProps) {
if (this.props.onUpdateOptions) {
this.props.onUpdateOptions(this._view, prevProps.viewOptions, this.props.viewOptions);
}
if (this._view.shouldViewRebuild && this._view.shouldViewRebuild(this.props.viewOptions)) {
this._rebuildView();
}
}
componentWillUnmount() {
this._regionManager.destroy();
}
getRegion() {
return this._hostRegion;
}
_rebuildView() {
if (!this.props.view) {
return;
}
const View = this.props.view;
this._view = new View(this.props.viewOptions);
this._hostRegion.show(this._view);
}
render() {
return <div className={this.props.className} ref={el => (this._el = el)} />;
}
}
MarionetteComponent.propTypes = {
view: PropTypes.func.isRequired,
viewOptions: PropTypes.object,
onUpdateOptions: PropTypes.func,
className: PropTypes.string
};
export default MarionetteComponent;