Skip to content

Commit 97fb2fa

Browse files
committed
examples: routing by a specified querysting key
1 parent 886a7f1 commit 97fb2fa

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

examples/querystring-key/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Custom routing environment
2+
3+
This example demonstrates how to route using custom environment. It implements
4+
tabs widget by storing active tab state in the `tab` key in the querysting.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../host.html

examples/querystring-key/index.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* @jsx React.DOM
3+
*/
4+
"use strict";
5+
6+
var qs = require('querystring');
7+
var React = require('react');
8+
var Router = require('../../');
9+
var Environment = require('../../lib/Environment');
10+
11+
var Locations = Router.Locations;
12+
var Location = Router.Location;
13+
var Link = Router.Link;
14+
15+
16+
function QueryStringKeyEnvironment(key) {
17+
this.key = key;
18+
Environment.HashEnvironment.call(this);
19+
}
20+
21+
QueryStringKeyEnvironment.prototype = Object.create(Environment.HashEnvironment.prototype);
22+
QueryStringKeyEnvironment.prototype.constructor = QueryStringKeyEnvironment;
23+
24+
QueryStringKeyEnvironment.prototype.getPath = function() {
25+
var path = Environment.HashEnvironment.prototype.getPath.call(this);
26+
if (path.indexOf('?') === -1) {
27+
return '/';
28+
}
29+
var query = qs.parse(path.split('?')[1] || '');
30+
return query[this.key] ? '/' + query[this.key] : '/';
31+
};
32+
33+
QueryStringKeyEnvironment.prototype.pushState = function(path, navigation) {
34+
path = this.updatedPath(path);
35+
Environment.HashEnvironment.prototype.pushState.call(this, path, navigation);
36+
}
37+
38+
QueryStringKeyEnvironment.prototype.replaceState = function(path, navigation) {
39+
path = this.updatedPath(path);
40+
Environment.HashEnvironment.prototype.replaceState.call(this, path, navigation);
41+
}
42+
43+
QueryStringKeyEnvironment.prototype.updatedPath = function(value) {
44+
var path = Environment.HashEnvironment.prototype.getPath.call(this);
45+
if (path.indexOf('?') === -1) {
46+
var query = {};
47+
query[this.key] = value.slice(1);
48+
return '/?' + qs.stringify(query);
49+
} else {
50+
var splitted = path.split('?');
51+
var query = qs.parse(splitted[1] || '');
52+
query[this.key] = value.slice(1);
53+
return splitted[0] + '?' + qs.stringify(query);
54+
}
55+
}
56+
57+
var Context = {
58+
mixins: [Router.RouterMixin],
59+
60+
getDefaultProps: function() {
61+
return {
62+
contextual: true,
63+
};
64+
},
65+
66+
getRoutes: function() {
67+
return [{path: '*', handler: null}];
68+
}
69+
};
70+
71+
var Tabs = React.createClass({
72+
73+
mixins: [Context],
74+
75+
getDefaultProps: function() {
76+
return {
77+
environment: new QueryStringKeyEnvironment(this.props.routeBy || 'tab')
78+
}
79+
},
80+
81+
render: function() {
82+
var links = [];
83+
var locations = [];
84+
85+
this.props.tabs.forEach((tab, idx) => {
86+
var href = '/' + (tab.id || idx);
87+
88+
links.push(Link({key: href, href: href}, tab.name));
89+
locations.push(Location({path: href, handler: tab.handler}));
90+
});
91+
92+
return (
93+
<div>
94+
<div className="nav">{links}</div>
95+
<Locations contextual className="content">{locations}</Locations>
96+
</div>
97+
);
98+
}
99+
});
100+
101+
var Tab1 = React.createClass({
102+
103+
render: function() {
104+
return <div>Tab1</div>;
105+
}
106+
});
107+
108+
var Tab2 = React.createClass({
109+
110+
render: function() {
111+
return <div>Tab2</div>;
112+
}
113+
});
114+
115+
var app = Tabs({
116+
hash: true,
117+
tabs: [
118+
{name: 'Tab 1', handler: Tab1},
119+
{name: 'Tab 2', handler: Tab2}
120+
]
121+
});
122+
123+
React.renderComponent(app, document.getElementById('host'));

0 commit comments

Comments
 (0)