Pages

Tuesday, September 6, 2016

inmadusa.com [Part 1]: Building a simple router with Path.js

After sampling several javascript routing solutions I settled for path.js. It is simple and perfect for my needs. When the site loads, the router is setup like this:
Path.map('#/:lang/:section').to(function() {
   var section = this.params['section'];
   var lang = this.params['lang'];

   // get the resources for the language.

   // build the proper HTML for the route.

   // load the content into the DOM.
});

Path.root("#/es/inicio");

Path.listen();

As you can see a route is created with parameters for the language and which section to load. A default route is then setup for the home section with spanish language and then the router starts listening.

That's all there is to it, the next post will go over using a template engine to load each section. In the mean time, you can visit the live site or check the source code.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.