9951 explained code solutions for 126 technologies


backbone.jsHow can I use Backbone.js routing to create a single page application?


Backbone.js routing is a powerful way to create a single page application. It allows developers to create a single page app that can respond to URL changes and update the page content accordingly. To use Backbone.js routing, one must first create a Router object and then define routes for each page.

// Create a router
var AppRouter = Backbone.Router.extend({
    routes: {
        "home": "showHome",
        "about": "showAbout"
    },
    showHome: function(){
        // do something when the home page is requested
    },
    showAbout: function(){
        // do something when the about page is requested
    }
});

// Create an instance of the router
var appRouter = new AppRouter();

// Start the router
Backbone.history.start();

This code creates a router object with two routes, home and about. When a route is requested, the corresponding function will be called. This is how Backbone.js routing allows developers to create a single page application.

In addition, developers can also use the navigate function to update the URL and trigger the route functions.

// Execute the about route
appRouter.navigate("about", {trigger: true});

This code will update the URL to example.com/about and trigger the showAbout function.

In summary, Backbone.js routing is a powerful way to create a single page application. It allows developers to create routes and trigger functions when a route is requested.

Helpful links

Edit this code on GitHub