cerebral-router

0.9.2 • Public • Published

cerebral-router

An opinionated URL change handler for Cerebral

NPM version Build status Test coverage bitHound Score

Install

npm install cerebral-router

How to use

When you build your Cerebral application you do not have to think about routing at all. You only trigger signals that brings your application into the correct state. Let us imagine an application that can open a messages page, and also open specific messages.

 
import controller from './controller.js';
import homeOpened from './signals/homeOpened';
import messagesOpened from './signals/messagesOpened';
import messageOpened from './signals/messageOpened';
 
controller.signal('messagesOpened', messagesOpened);
controller.signal('messageOpened', messageOpened);

When we want to open the messages we call the signal:

 
onMessagesClick() {
  this.props.signals.messagesOpened();
}

When we want to open a single message we call the signal and pass a payload:

 
onMessageClick(id) {
  this.props.signals.messageOpened({
    id: id
  });
}

The signature of a state change is the signal and the payload passed. We can bind this signature to a route. Lets imagine we have implemented our whole application and it works great, we just need to update the addressbar with a url representing the current state of the application.

So let us also add a homeOpened signal so that we handle the root url as well.

 
import Router from 'cerebral-router';
import controller from './controller.js';
import homeOpened from './signals/homeOpened';
import messagesOpened from './signals/messagesOpened';
import messageOpened from './signals/messageOpened';
 
controller.signal('homeOpened', homeOpened);
controller.signal('messagesOpened', messagesOpened);
controller.signal('messageOpened', messageOpened);
 
Router(controller, {
  '/': 'homeOpened',
  '/messages': 'messagesOpened',
  '/messages/:id': 'messageOpened'
}, {
  mapper: { query: true } // Read about this below
});

Initial url would be handled automatically during application bootstrap if you are using cerebral-react (in container's componentDidMount method) or cerebral-angular (module's run section) packages. Otherwise you should call trigger method to ensure that initial url is handled.

The router checks the url and fires the signal related to the url. The url will be parsed and any payload will be passed on the signal. That means if you go to example.com/messages/123 it will trigger the messageOpened signal with the payload {id: '123'}.

But if you click a message in the list it will also trigger the messageOpened signal with the payload {id: '456'} and now the url will also update to example.com/messages/456.

So it works both ways!

The important thing to understand here is that your application does not trigger urls to change its state. It triggers signals. Then you bind a route to a signal to allow a url to trigger the signal as well.

That means:

 
// Going to url
"example.com/messages/456"
 
// Is exactly the same as
this.props.signals.messageOpened({
  id: '456'
});

Diving into the app from a url

In the example above, when navigating in the app, you have to go to /messages before you can go to /messages/456. But when you expose urls you could go directly to /messages/456. So how do you handle that?

 
...
controller.signal('messageOpened', [...messagesOpened, ...messageOpened]);
 
Router(controller, {
  '/': 'homeOpened',
  '/messages': 'messagesOpened',
  '/messages/:id': 'messageOpened'
});

With Cerebral you are already used to composing chains and actions together and this is also effective when creating routes. Now you might say, "I do not want to load my messages every time I open a message!". There are multiple ways to handle this. It depends on when you want to load the messages.

But lets say you want to load them whenever you actually go to /messages. Inside your messagesOpened signal you can just check if there is an ID on the input. If there is an ID it means you are about to open a message, if not it means you are just opening the messages.

What about queries?

With Cerebral you get a very powerful way to use queries. But first we have to make a statement: "Queries are produced by your application, not by users". With this perspective we can do some wonderful things. Lets get back to opening our message. Inside the component opening the message we want to pass more than the ID of the message. We want to pass: {withComments: true}. So that when we load the message, we load it with comments.

 
onMessageClick(id) {
  this.props.signals.messageOpened({
    id: id,
    withComments: true
  });
}

Since this signal is bound to a url Cerebral router will automatically make this part of the query, turning your url into example.com/messages/123?withComments:true. That means if you refresh or pass the url to somebody else it will pass {id: '123', withComments: true} as the payload to the signal, opening the message in the exact same way, with the comments.

Notice here that we have withComments:true, not withComment=true. This is because Cerebral router uses the URLON project to create serializable queries. As you can see it is very powerful.

Tell me more about how routes matched

cerebral-router relies on url-mapper default behavior:

  • path-to-regexp library is used to define routes and payload parameters. It tweaked to preserve payload parameters with Number and Boolean types, prepending it with colon.
  • Payload parameters not defined in route part is mapped to query using URLON notation if mapper: { query: true } option was passed to router.

Routable part of url is extracted based on onlyHash and baseUrl options provided to router.

Router(controller, {
  '/': 'homeOpened',
  '/messages': 'messagesOpened',
  '/messages/:id': 'messageOpened'
}, {
  onlyHash: true,
  baseUrl: '/path',
  mapper: { query: true }
});

With given config and https://example.com/path#/messages/123?withComments:true url /messages/123?withComments:true is routable part. Then url-mapper matches it to route /messages/:id and extracts payload {id: '123', withComments: true}.

I want my own scheme for routes as well as queries

Feel free to implement compileFn which return your own parse and stringify methods for given route. The only recommendation is: parse method for previously stringified payload should result the same payload. Just like JSON.parse(JSON.stringify(object)). Create an issue if you need this now since cerebral-router had to be patched to support custom mapper.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.9.23latest
1.0.0-b-alpha.c00bd8f22next
1.0.0-b-alpha.3aa448151test
1.0.0-4-router-great-again1great

Version History

VersionDownloads (Last 7 Days)Published
1.0.0-b-alpha.c00bd8f22
1.0.0-b-alpha.a2e68a011
1.0.0-b-alpha.a54303bb1
1.0.0-b-alpha.b517b73b1
1.0.0-b-alpha.740f79204
1.0.0-b-alpha.54e312b71
1.0.0-b-alpha.7904e2901
1.0.0-b-alpha.e8d731051
1.0.0-b-alpha.99699ebd1
1.0.0-b-alpha.1319d5211
1.0.0-b-alpha.6975f2a31
1.0.0-b-alpha.5c33a4121
1.0.0-b-alpha.e83c48991
1.0.0-b-alpha.8f71faff1
1.0.0-b-alpha.dea777851
1.0.0-b-alpha.5ee0668b1
1.0.0-b-alpha.5c6515561
1.0.0-b-alpha.6d7678484
1.0.0-b-alpha.27d3876f1
1.0.0-b-alpha.b56ea2a81
1.0.0-b-alpha.a7e09a781
1.0.0-b-alpha.619dc5f51
1.0.0-b-alpha.56ce50da1
1.0.0-b-alpha.44b98c711
1.0.0-b-alpha.e783cbc01
1.0.0-b-alpha.2e8b97971
1.0.0-b-alpha.133f9d171
1.0.0-b-alpha.70390ee31
1.0.0-b-alpha.2ba697891
1.0.0-b-alpha.77c894f21
1.0.0-b-alpha.ebd1822c1
1.0.0-b-alpha.7567ea2f1
1.0.0-b-alpha.4fc783b61
1.0.0-b-alpha.371596111
1.0.0-b-alpha.0e8b027d1
1.0.0-b-alpha.022686d21
1.0.0-b-alpha.22a274811
1.0.0-b-alpha.cdf3a89e1
1.0.0-b-alpha.2eb700e41
1.0.0-b-alpha.0c37d8681
1.0.0-b-alpha.1a6352a21
1.0.0-b-alpha.d93b07d81
1.0.0-b-alpha.714f5cf81
1.0.0-b-alpha.49a7e38e1
1.0.0-b-alpha.fa56b7f51
1.0.0-b-alpha.5f6212a51
1.0.0-b-alpha.337127131
1.0.0-b-alpha.2b93a1e61
1.0.0-b-alpha.a46cc4f32
1.0.0-b-alpha.d4b789f71
1.0.0-b-alpha.7788270c1
1.0.0-b-alpha.2cdbe7234
1.0.0-b-alpha.39c2c2421
1.0.0-b-alpha.067540ce1
1.0.0-b-alpha.860cd2844
1.0.0-b-alpha.10a41abb1
1.0.0-b-alpha.594e62821
1.0.0-b-alpha.220b9d6d1
1.0.0-b-alpha.305f021e1
1.0.0-b-alpha.00e865bb1
1.0.0-b-alpha.ebdf3add1
1.0.0-b-alpha.06dadda61
1.0.0-b-alpha.39c3d8df1
1.0.0-b-alpha.6f1668ac1
1.0.0-b-alpha.c8499f181
1.0.0-b-alpha.d6efd5f32
1.0.0-b-alpha.d68188642
1.0.0-b-alpha.f3fd76724
1.0.0-b-alpha.228849641
1.0.0-b-alpha.fab0e4d11
1.0.0-b-alpha.2e63f1811
1.0.0-b-alpha.a8c5a3ed1
1.0.0-b-alpha.d8335fdc1
1.0.0-b-alpha.101044221
1.0.0-b-alpha.d465c0331
1.0.0-b-alpha.33e5be121
1.0.0-b-alpha.5c0f8e5d1
1.0.0-b-alpha.1c722af31
1.0.0-b-alpha.4136e51e1
1.0.0-b-alpha.3aa448151
1.0.0-b-alpha.049072e02
1.0.0-b-alpha.78c7eb7e1
1.0.0-4-router-great-again1
1.0.0-b-alpha.9b9183421
1.0.0-b-alpha.95daa3631
1.0.0-b-alpha.bed273e91
1.0.0-b-alpha.e6ad42911
1.0.0-b-alpha.0e4c147a1
1.0.0-b-alpha.757a81fc1
1.0.0-b-alpha.c5da9d991
1.0.0-b-alpha.b81ac195-alpha.b81ac1951
1.0.0-b-alpha.b81ac1951
1.0.0-b-alpha.32316d671
1.0.0-b-alpha.c5badd321
1.0.0-b-alpha.fb1baf7b2
1.0.0-b-alpha.544b7c3c1
1.0.0-b-alpha.e564a768-alpha.e564a7681
1.0.0-b-alpha.e564a7681
1.0.0-b-alpha.b79516993
1.0.0-b-alpha.f8b524d11
1.0.0-b-alpha.4690d3a22
1.0.0-b-alpha.0a2241371
1.0.0-b-alpha.23cc3ebf1
1.0.0-b-alpha.b3e4c51f1
1.0.0-b-alpha.b2f2b6661
1.0.0-b-alpha.2d615ece1
1.0.0-b-alpha.1e9a3f661
1.0.0-3-router-great-again1
1.0.0-2-router-great-again1
1.0.0-b-alpha.c33fb0661
1.0.0-b-alpha.e27373fd1
1.0.0-b-alpha.547405cb1
1.0.0-b-alpha.1a22c7011
1.0.0-b-alpha.c7c326bf2
1.0.0-b-alpha.396105881
1.0.0-b-alpha.6eab75d61
1.0.0-b-alpha.d1c1c8d61
1.0.0-b-alpha.3b97c05c1
1.0.0-b-alpha.4ca9aef91
1.0.0-b-alpha.e52912961
1.0.0-b-alpha.7cec7ee71
1.0.0-b-alpha.713f066c2
1.0.0-b-alpha.8dd85aae1
1.0.0-b-alpha.b2be293f1
1.0.0-0-alpha.9da455351
1.0.0-0-alpha.67b0ddcb1
1.0.0-0-alpha.ff4ac2b11
1.0.0-0-alpha.d3127a2a1
1.0.0-0-alpha.3ef54b371
1.0.0-0-alpha.6c3f1ec01
1.0.0-0-alpha.dd3d77b51
1.0.0-0-alpha.ea26c4f91
1.0.0-1-router-great-again1
1.0.0-0-router-great-again2
1.0.0-0-alpha.3fcc3e881
1.0.0-0-alpha.61cfe1a61
1.0.0-0-alpha.5b8ecb8f1
1.0.0-0-alpha.25e07e3c1
1.0.0-0-alpha.ec8ba3a11
1.0.0-0-alpha.fc9525031
1.0.0-0-alpha.a4b3173e2
1.0.0-0-alpha.5ff3391a1
1.0.0-0-alpha.77f9a41b1
1.0.0-0-alpha.603cfe452
1.0.0-0-alpha.1788b5a71
1.0.0-0-alpha.d61a22541
1.0.0-0-alpha.65efef022
1.0.0-0-alpha.d81c2c441
1.0.0-0-alpha.c49c1d861
1.0.0-0-alpha.e514028a1
1.0.0-0-alpha.2d565fc11
1.0.0-0-alpha.336321792
1.0.0-0-alpha.fe8128f51
1.0.0-0-alpha.31e15eed1
1.0.0-0-alpha.ac5849d01
1.0.0-0-alpha.582a630a2
1.0.0-0-alpha.2f993c5f1
1.0.0-0-alpha.1a230f8a1
1.0.0-0-alpha.f06009ab2
1.0.0-0-alpha.2d6d56671
1.0.0-0-alpha.9a0b482e1
1.0.0-0-alpha.d45611d81
1.0.0-0-alpha.e6bb6bed1
1.0.0-0-alpha.012818d61
1.0.0-0-alpha.802e78b61
1.0.0-0-alpha.3ec5bb822
1.0.0-0-alpha.0aecaf3d2
1.0.0-0-alpha.d63a57a31
1.0.0-0-alpha.ad9f6d4c1
1.0.0-0-alpha.7b0927fe1
1.0.0-0-alpha.37a909bf1
1.0.0-0-alpha.f71c7af02
1.0.0-0-alpha.acfe7e521
1.0.0-0-alpha.6d195d971
1.0.0-0-alpha.33ff39b01
1.0.0-0-alpha.36dab8021
1.0.0-0-alpha.5a3d129e1
1.0.0-0-alpha.23fa0f3b1
1.0.0-0-alpha.5f134ae31
1.0.0-0-alpha.4bb8329b1
1.0.0-0-alpha.d9cf0f7e1
1.0.0-0-alpha.e0aaa3ec1
1.0.0-0-alpha.e0efed821
1.0.0-0-alpha.3dc2ae791
1.0.0-0-alpha.23040ee41
1.0.0-0-alpha.212f71f41
1.0.0-0-alpha.21ed471e1
1.0.0-0-alpha.764e42101
1.0.0-0-alpha.dca891611
1.0.0-alpha.2-alpha.ed1e45681
1.0.0-alpha.21
1.0.0-alpha.11
0.9.23
0.9.11
0.9.01
0.8.01
0.7.41
0.7.31
0.7.21
0.7.11
0.7.01
0.6.11
0.6.02
0.5.11
0.5.01
0.4.22
0.4.11
0.4.01
0.3.141
0.3.131
0.3.121
0.3.111
0.3.101
0.3.91
0.3.81
0.3.71
0.3.61
0.3.51
0.3.41
0.3.31
0.3.21
0.3.11
0.3.01
0.2.01
0.1.01

Package Sidebar

Install

npm i cerebral-router

Weekly Downloads

265

Version

0.9.2

License

MIT

Last publish

Collaborators

  • cerebral.js
  • christianalfoni
  • guria