-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
fresh-bundle.js
203 lines (193 loc) · 5.1 KB
/
fresh-bundle.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var Fresh = {
mixins: {},
components: {},
getComponentByName: function(name) {
return this.components[name];
},
start: function(rootProps, container) {
var component = this.getComponentByName(rootProps.component),
content;
if (!component) {
return;
}
React.renderComponent(component(_.clone(rootProps)), container);
}
};
// Enable Node.js compatibility
if (typeof module !== 'undefined' && module.exports) {
var React = require('react-tools').React,
_ = require('underscore'),
$ = require('jquery');
module.exports = Fresh;
}
Fresh.serialize = {
getPropsFromQueryString: function(queryString) {
var props = {};
if (queryString.length) {
var pairs = queryString.split('&'),
parts,
key,
value;
for (var i = 0; i < pairs.length; i++) {
parts = pairs[i].split('=');
key = parts[0];
value = parts[1];
if (key == 'state') {
value = JSON.parse(decodeURIComponent(value));
}
props[key] = value;
}
}
return props;
},
getQueryStringFromProps: function(props) {
var parts = [],
value;
for (var key in props) {
value = props[key];
// Objects can be embedded in a query string as well
if (typeof value == 'object') {
value = encodeURIComponent(JSON.stringify(value));
}
parts.push(key + '=' + value);
}
return parts.join('&');
}
};
Fresh.url = {
getParams: function () {
return Fresh.serialize.getPropsFromQueryString(
window.location.search.substr(1));
}
};
Fresh.mixins.DataManager = {
fetchDataFromServer: function() {
var url = this.props.data;
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
this.receiveDataFromServer(data);
}.bind(this),
error: function(xhr, status, err) {
console.error(url, status, err.toString());
}.bind(this)
});
},
receiveDataFromServer: function(data) {
this.setState({data: data});
},
getDefaultProps: function() {
return {
// Enable polling by setting a value bigger than zero, in ms
pollInterval: 0
};
},
componentWillMount: function() {
// The data prop points to a source of data than will extend the initial
// state of the component, once it will be fetched
// TODO: Fetch data again when props change at componentWillReceiveProps
if (!this.props.data) {
return;
}
this.fetchDataFromServer();
if (this.props.pollInterval) {
this.setInterval(this.fetchDataFromServer, this.props.pollInterval);
}
}
};
Fresh.mixins.PersistState = {
generatePropsSnapshot: function() {
var defaultProps = this.getDefaultProps ? this.getDefaultProps() : {},
props = {},
value,
state;
for (var key in this.props) {
value = this.props[key];
// Ignore "system" props
if (key == '__owner__' ||
// Current state should be used instead of initial one
key == 'state') {
continue;
}
// No point in embedding default props
if (defaultProps.hasOwnProperty(key) && defaultProps[key] == value) {
continue;
}
props[key] = value;
}
state = _.clone(this.state);
// No need to embed data if we have an URL to fetch it from
if (state && state.data && props.data) {
delete state.data;
}
if (!_.isEmpty(state)) {
props.state = state;
}
return props;
},
getUriQueryString: function() {
return Fresh.serialize.getQueryStringFromProps(
this.generatePropsSnapshot());
},
componentWillMount: function() {
// Allow passing a serialized snapshot of a state through the props
// TODO: Replace state when props change at componentWillReceiveProps
if (this.props.state) {
this.replaceState(this.props.state);
}
}
};
Fresh.mixins.SetInterval = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.map(clearInterval);
}
};
/** @jsx React.DOM */
Fresh.components.Author = React.createClass({
/**
* Input: {
* component: 'Author',
* name: 'Dan Ciotu'
* }
*/
mixins: [Fresh.mixins.SetInterval,
Fresh.mixins.PersistState,
Fresh.mixins.DataManager],
render: function() {
return (
React.DOM.div(null, this.state.data.name)
);
}
});
/** @jsx React.DOM */
Fresh.components.List = React.createClass({
/**
* Input: {
* component: 'List',
* data: 'http://localhost/static/users.json'
* }
*/
mixins: [Fresh.mixins.SetInterval,
Fresh.mixins.PersistState,
Fresh.mixins.DataManager],
getInitialState: function() {
return {data: []};
},
render: function() {
return (
React.DOM.ul( {className:"List"},
this.state.data.map(function(item, index) {
var itemComponent = Fresh.getComponentByName(item.component);
return React.DOM.li( {key:index}, itemComponent(_.clone(item)))
})
)
);
}
});