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 |
|
9 | Documentation for the 1.x API, can be found [here](https://github.com/featurist/plastiq-router/tree/v1).
|
10 |
|
11 | ## install
|
12 |
|
13 | ```bash
|
14 | npm install plastiq-router
|
15 | ```
|
16 |
|
17 | ## example
|
18 |
|
19 | You 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
|
24 | var plastiq = require('plastiq');
|
25 | var h = plastiq.html;
|
26 | var router = require('plastiq-router');
|
27 |
|
28 | var routes = {
|
29 | root: router.route('/'),
|
30 | document: router.route('/document/:documentId'),
|
31 | search: router.route('/search')
|
32 | };
|
33 |
|
34 | var 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 |
|
42 | function renderDocument(d) {
|
43 | return h('.document',
|
44 | h('h1', d.title),
|
45 | h('.content', d.content)
|
46 | );
|
47 | }
|
48 |
|
49 | router.start();
|
50 |
|
51 | function 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 |
|
83 | plastiq.append(document.body, render, model);
|
84 | ```
|
85 |
|
86 | # API
|
87 |
|
88 | ## start
|
89 |
|
90 | ```js
|
91 | router.start([options]);
|
92 | ```
|
93 |
|
94 | Starts 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
|
101 | router.stop();
|
102 | ```
|
103 |
|
104 | Stops the router, removing event handlers for navigation. This is particularly useful in test teardown.
|
105 |
|
106 | ## create a route
|
107 |
|
108 | ```js
|
109 | var 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 |
|
117 | Routes 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
|
122 | route(function (params) {
|
123 | return vdom;
|
124 | });
|
125 | ```
|
126 |
|
127 | If 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
|
134 | route(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
|
153 | var 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
|
165 | routeInstance.href
|
166 | ```
|
167 |
|
168 | The root-relative HREF of the route - of the form `/path`.
|
169 |
|
170 | ### active
|
171 |
|
172 | ```js
|
173 | routeInstance.active
|
174 | ```
|
175 |
|
176 | Whether the route is currently active.
|
177 |
|
178 | ### push, replace
|
179 |
|
180 | ```js
|
181 | routeInstance.push()
|
182 | routeInstance.replace()
|
183 | ```
|
184 |
|
185 | Either 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
|
190 | routeInstance.a([options], contents, ...)
|
191 | ```
|
192 |
|
193 | Generates 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.
|