UNPKG

1.16 kBJavaScriptView Raw
1import querystring from 'querystring';
2import App from './app';
3
4class ClientApp extends App {
5 constructor (props = {}) {
6 super(props);
7
8 this.state = props.state || {};
9 }
10
11 // Server uses express sessions; on the client, we'll persist state in memory.
12 getState (prop) {
13 if (prop) {
14 return this.state[prop];
15 } else if (typeof prop === 'undefined') {
16 return this.state;
17 }
18 }
19
20 setState (prop, val) {
21 this.state[prop] = val;
22 return val;
23 }
24
25 resetState (state) {
26 this.state = state || {};
27 }
28
29 buildRequest (url) {
30 var splitUrl = url.split('?');
31 var query = {};
32 var url = url || '/';
33
34 var pathName = this.fullPathName();
35
36 if(splitUrl.length > 1) {
37 url = splitUrl[0] || '/';
38 query = querystring.parse(splitUrl[1] || '');
39 }
40
41 var req = {
42 path: url,
43 url: url,
44 method: 'GET',
45 renderSynchronous: false,
46 useCache: true,
47 query: query,
48 headers: {
49 referer: pathName,
50 },
51 }
52
53 return req;
54 }
55
56 fullPathName () {
57 return document.location.pathname + document.location.search;
58 }
59}
60
61module.exports = ClientApp;