-
Notifications
You must be signed in to change notification settings - Fork 1
/
blaze-as-react.js
78 lines (68 loc) · 2.33 KB
/
blaze-as-react.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
// Test that we do not overwrite some existing properties
if (Template.AsReactComponent) {
throw new Meteor.Error("4commerce:blaze-as-react", "Sorry, but you have already defined a method named Template.AsReactComponent!");
}
// Define as function to create a React component out of
// a Blaze template. Use the result of the method call
// later on inside you React components
//
// e.g.:
//
// var Foo = Template.AsReactComponent('foo');
//
// <Foo data="bar" />
//
Template.AsReactComponent = function (template) {
// create and return a new React component
return React.createClass({
// Leave full control to Blaze once component is in use
shouldComponentUpdate: function() {
return false;
},
// append props to templates data
componentWillReceiveProps: function(props) {
_.extend(this.blazeView.dataVar.curValue, props);
// signal tracker
this.blazeView.dataVar.dep.changed();
},
// insert this component to DOM
componentDidMount: function() {
var componentNode = React.findDOMNode(this);
// get name of template from method call or template property
template = template || this.props.template;
// check for existing template
if (template && Template[template]) {
// save successfull rendered view
this.setState({ blazeView: Blaze.renderWithData(Template[template], this.props, componentNode) });
} else {
// drop an error
throw new Meteor.Error("Template.ToReact", "Template " + template + "is missing.");
}
},
// check to remove view from Blaze if was created
componentWillUnmount: function() {
if (this.state.blazeView) {
Blaze.remove(this.state.blazeView);
// unset state
this.setState({ blazeView: undefined });
}
},
// simple render this component
render: function() {
return React.createElement("div", null);
}
});
}
// Test that we do not overwrite some existing properties
if (React.BlazeView) {
throw new Meteor.Error("4commerce:blaze-as-react", "Sorry, but you have already defined a method named React.BlazeView!");
}
// Define as property to global React object to
// simple allow inserting of Blaze templates
// inside React compnonets
//
// e.g.:
//
// <React.BlazeView template="foo" data="bar" />
//
React.BlazeView = Template.AsReactComponent();