Routing

Essentially, routes in Lava allow you to control browser navigation without having to leave the page. For example, you could have a link that executes a route instead of navigating away from the page and therefore the application.

Routes are very simple yet powerful when used in Lava. To create a route you would use this simple syntax:

Lava.Routes.add('RouteName', functionToExecute);

You can then go to this route programatically by making the following call:

Lava.Routes.go('RouteName');

Whenever a registered route is navigated to manually (user types it into the address bar), the routing system is aware of this and executes the corresponding function.

When you "go" to a route by calling the Routes.go method, an entry is created in the browsers history by default. This makes it possible for users to go through the browser history with the forward/back buttons in the browser.

You can suppress adding the route into history by setting the ignorePushState argument in the go method to true:

Lava.Routes.go('RouteName', true);

Here is a breakdown of the Lava.Routes object:

Methods

add (string route, function action): Adds a new route to the route collection and registers it as a valid route. If there are any failures, the operation is gracefully aborted.

get (string route): Gets the route object with the specified name (route names are case sensitive).
Returns a route with the following definition: { name: 'RouteName', path: 'full path of the route', action: function that executes when the route is navigated to }.

getCurrent: Gets the current route object or undefined.
Returns a route object.

go (string route, bool ignorePushState): Navigates to the specified route, optionally suppressing the addition of the route into the browsers history.



Last edited Jan 25, 2013 at 5:06 PM by tjscience, version 2

Comments

No comments yet.