Swallow route allows you to create "single page" applications using Hashbangs and HTML5 pushState, purely javascript.
// You can use any methods as actions. You can define them as I do below,
// assign them to variables, or use anonymous functions. The choice is yours.
function notFound() {
renderView('404', swallowJsContainer);
}
function notFound() {
$("#output .content").html("404 Not Found");
}
Below we define our routes. You'll notice that we only define three routes, even though there are four links. Each route has an action assigned to it (via the `to` method, as well as an `enter` method. The `enter` method is called before the route is performed, which allows you to do any setup you need (changes classes, performing AJAX calls, adding animations, etc.
Path.map("/users").to(function () {
renderView('layout_user', swallowJsContainer);
}).enter(this_function_is_called);
Path.map("/about").to(function () {
renderView('layout_about', swallowJsContainer);
}).enter(this_function_is_called);
Path.map("/contact").to(function () {
renderView('layout_contact', swallowJsContainer);
}).enter(this_function_is_called);
function this_function_is_called() {
// You can put some code in here to do fancy DOM transitions, such as fade-out or slide-in.
}
This route will match things such as "#/users/1", "#/users/500", and "#/users/mike"
. Inside the action of that route, you have access to the parameters via the `this.params`
object.
Path.map("#/users/:user_id").to(function(){
var user_id = this.params["user_id"];
logMessage(user_id);
});
This is a route with optional components. Optional components in a route are contained within brackets. The route below will match both "#/about" and "#/about/olatunde"
.
Path.map("#/about(/author)").to(function(){
logMessage(About & About/Author share a route!);
});
The most important parts of the Swallow.Js Framework are the simple included set of different functions. Swallow.Js is built on these components PathJs for routing, MustacheJs for view and depend more on Firebase for data-source, and also build-in server side request for your regular JSON calls.