Pro

What is a Pro Router?

Let's explain basics of frontend routing in Pro - which uses a singleton service.

May 24, 2017

Let us look on an example URL:

http://www.example.com/section/post/1/comment/123
On any site we can play in JS console with a location object. So we'll see an url contains of a protocol, hostname and a pathname. Saying about pathname it's this part:
/section/post/1/comment/123
What we can say about this string it has sections separated by /. It means we can split by it and receive an array:
['section', 'post', '1', 'comment', '123']
Let's agree that first element section is a view name. Then make from the other elements an array of pairs:
[['post', '1'], ['comment', '123']]
Which can be easily translated to a hash:
{ post: 1, comment: 123 }
You can treat this hash as an equivalent of Ruby on Rails params object which you use in Rails controllers. But here you use it in frontend components. Play it in console using those methods:
Router.view
and
Router.param('post')
and
Router.write({ post: 2 })
or
Router.write('post', 2)
Read the code here. You can use it with any single page application outside Pro. If you need help just drop me a line. Have fun!


Do you want to get Pro tips?




CONTACT ME AT MAD@MANIAK.PRO. I NEED YOUR QUESTIONS.