UNPKG

4.73 kBMarkdownView Raw
1# plastiq-router
2
3* incredibly simple
4* generate links from routes
5* route parameters can be bound to the model
6
7## rewrite + API simplication
8
9Documentation for the 1.x API, can be found [here](https://github.com/featurist/plastiq-router/tree/v1).
10
11## install
12
13```bash
14npm install plastiq-router
15```
16
17## example
18
19You can see this example in action [here](http://www.featurist.co.uk/plastiq-router/example/)
20
21* On the search page, notice how the URL changes as you type the search query.
22
23```js
24var plastiq = require('plastiq');
25var h = plastiq.html;
26var router = require('plastiq-router');
27
28var routes = {
29 root: router.route('/'),
30 document: router.route('/document/:documentId'),
31 search: router.route('/search')
32};
33
34var model = {
35 documents: [
36 {id: 0, title: 'One', content: 'With just one polka dot, nothing can be achieved...'},
37 {id: 1, title: 'Two', content: 'Sometimes I am two people. Johnny is the nice one...'},
38 {id: 2, title: 'Three', content: 'To be stupid, selfish, and have good health are three requirements for happiness...'}
39 ]
40};
41
42function renderDocument(d) {
43 return h('.document',
44 h('h1', d.title),
45 h('.content', d.content)
46 );
47}
48
49router.start();
50
51function render(model) {
52 return h('div',
53 routes.root().a('Home'),
54 ' | ',
55 routes.search().a('Search'),
56 routes.root(function () {
57 return h('ol.documents',
58 model.documents.map(function (d, index) {
59 return h('li', routes.document({documentId: index}).a(d.title));
60 })
61 );
62 }),
63 routes.document(function (params) {
64 return renderDocument(model.documents[params.documentId]);
65 }),
66 routes.search({q: [model, 'query']}, function () {
67 var query = model.query? model.query.toLowerCase(): undefined;
68 return h('div',
69 h('h1', 'search'),
70 h('input', {type: 'text', binding: [model, 'query']}),
71 h('ol.results',
72 model.documents.filter(function (d) {
73 return query && d.title.toLowerCase().indexOf(query) >= 0 || d.content.toLowerCase().indexOf(query) >= 0;
74 }).map(function (d) {
75 return h('li', routes.document({documentId: d.id}).a(d.title));
76 })
77 )
78 );
79 })
80 );
81}
82
83plastiq.append(document.body, render, model);
84```
85
86# API
87
88## start
89
90```js
91router.start([options]);
92```
93
94Starts the router, adding event handlers for navigation.
95
96* `options.history` - a history driver, currently two supported: `router.historyApi` (the default) and `router.hash`.
97
98## stop
99
100```js
101router.stop();
102```
103
104Stops the router, removing event handlers for navigation. This is particularly useful in test teardown.
105
106## create a route
107
108```js
109var route = router.route(pattern);
110```
111
112* `pattern` - the path pattern: `/` or `/path`, or `/path/:id`, or `/path/:id/:path*`
113* `route` - the route, to be used in rendering, see below
114
115## render a route
116
117Routes can be rendered in two forms, passive and active. Passive routes do not modify the route parameters, active routes bind the model to the route parameters, effectively allowing the URL to change as the model changes.
118
119### passive routes
120
121```js
122route(function (params) {
123 return vdom;
124});
125```
126
127If the route is active, returns the `vdom` passing the `params` taken from the route to the function. If the route is not active, `undefined` is returned.
128
129* `params` - the params taken from the route, these can be from `:param` elements in the route pattern or query string parameters.
130
131### active routes
132
133```js
134route(bindings, function () {
135 return vdom;
136});
137```
138
139* `bindings` - how the model binds on to the route parameters, takes the form:
140
141 ```js
142 {
143 param1: [model, 'param1'],
144 param2: [model, 'param2']
145 }
146 ```
147
148 Where the object keys are the parameter names, and the values are the bindings onto the model.
149
150## route instances
151
152```js
153var routeInstance = route([params]);
154```
155
156* `params` - an optional object containing the parameters of the form:
157
158 ```js
159 {param1: 'param1 value', param2: 'param2 value'}
160 ```
161
162### href
163
164```js
165routeInstance.href
166```
167
168The root-relative HREF of the route - of the form `/path`.
169
170### active
171
172```js
173routeInstance.active
174```
175
176Whether the route is currently active.
177
178### push, replace
179
180```js
181routeInstance.push()
182routeInstance.replace()
183```
184
185Either push the route onto the history stack (using history.pushState) or replace the current URL (using history.replaceState). Replace only works with the router.historyApi driver, which is the default.
186
187### a, anchor, link
188
189```js
190routeInstance.a([options], contents, ...)
191```
192
193Generates virtual DOM for an anchor for the route, passing the arguments to `h('a', options, contents, ...)`, but with the correct `href` and `onclick` properties set.