Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:10:46+00:00 2026-06-09T02:10:46+00:00

I am building an application in backbone combining the modular and facade/mediator pub/sub patterns

  • 0

I am building an application in backbone combining the modular and facade/mediator pub/sub patterns from https://github.com/addyosmani/backbone-aura/ to send messages between modules in order to keep a clean codebase.

Grepping for Router in the entire app example for aura, I found only the readme file describing the ideal use of routers being part of modules themselves: “In Backbone.js terms, widgets are composed of Models, Views, Collections and Routers as well as any templates needed for the widget to rendered.”

So I tried a number of solutions to implement a scalable routing system (scalable meaning modules can specify their own subroutes), including a router module that accepts message set-route to set the route, and modules that listen to the route message. I’ve also used a sub-router per module. The problem seems that on initial page loads, because of the ‘async’ nature of messaging, the routes and their callbacks may not be defined by the time the global router parses the URL. You can imagine I maybe need to queue all messages before starting the router module.

I want to implement something clean and that makes sense. I was also thinking about potentially parsing all routes of all widgets first, then instantiating the router, which makes the router module a special case, and thus, shouldn’t be a part of the modules.

Should the Router be a module that uses messages, or should it be an extension, or some higher order piece of the architecture that is global that modules do have access to?

I know this is a loaded question, thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:10:48+00:00Added an answer on June 9, 2026 at 2:10 am

    There’s been a lot of debate over this issue, and I think a lot of that stemmed from what I think was confusion over Router formally being called Controller. By the time I started using Backbone late last year, the change was already made, but I believe lots of people had already built applications around the router being a controller. I never agreed with that. To me – relying on experience of having built a proprietary MVC engine similar to Backbone long before Backbone was created – a router was simply a history manager component.

    So to address your particular issue on deciding how best to implement a router, take into consideration a few things:

    1. First of all, a router is not a necessary component of an application. You could do without a router and still navigate to the various pages or screens of the app.
    2. A router is not a controller in that its primary function is to manage history. Though you could embed application business logic within a router, I’ve always found that that muddies the waters of what a router really does.
    3. By making a router a component of an application, you create a better separation of concerns, and can have a much more effective pub/sub type of arrangement.

    Below is code for a router module I use in my apps that follows the mediator, pub/sub patterns:

    /**
     * The idea behind this component is simply to relegate Backbone.Router to
     * doing what it does best: History Management. All it is responsible for
     * is two things:
     *
     * 1. Update the URL if router.navigate is invoked
     * 2. Trigger a routeChanged event if the URL was updated either by a bookmark or
     *    typed in by a user.
     */
    define(function () {
        return Backbone.Router.extend({
            initialize : function (map) {
                this._reversedMap = this.reverseModuleMap(map);
            },
            routes:{
                '*actions':'notify'
            },
            notify:function (actions) {
                var args = arguments;
                this.trigger("routeChanged", {command:actions});
            },
            /**
             * Override Backbone.Router.navigate. Default is to pass a router fragment, but for
             * our uses, we'll translate the "route" into a module mapping so that the controller
             * will know which module to display.
             * @param param
             * @param options
             */
            navigate:function (param, options) {
                //console.log('navigate', param);
                if(!param.suppressNavigate && param.actionCommand)  {
                    Backbone.Router.prototype.navigate.call(this, this._reversedMap[param.actionCommand]);
                } else if(!param.actionCommand) {
                    Backbone.Router.prototype.navigate.call(this, param, options);
                }
            },
            /**
             * this function simply reverses the key and value of the "map"
              * @param map
             */
            reverseModuleMap:function (map) {
                var newMap = {};
                _.each(map, function (value, key) {
                    newMap[value] = key;
                });
                // reversed key and value
                return newMap;
            }
        });
    });
    

    Then when I instantiate the component, I pass it a map so that my controller knows what module to navigate to:

    this._router = new Router({
        _default: 'moduleA',
        sites : 'moduleA',
        apps : 'moduleB'
    });
    this._router.on('routeChanged', this.handleRouteChange, this);
    

    The most important thing I’ve found with this is that it keeps the code nice and simple, and allows me to focus on business logic in my controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the step-by-step building application from the http://msdn.microsoft.com/en-us/library/hh202876(v=vs.92).aspx but the .sdf file won't
I'm building a backbone.js application, and don't quite know where to put my templates.
I am building a small administrative application, and I want to use Backbone.js on
I'm building an application that needs to open self-signed HTTPS SSL URLs in java.
I'm building a Backbone application and I'm observing some behaviour I can't place. Consider
I am building an application using the Backbone Boilerplate , and am having some
I'm building a web application with various visualization components, made up of Backbone.js Models
If I am building an application with backbone.js, what is the best tool or
I'm building a single page, offline html5 web application using jquery mobile, backbone, underscore
I'm building one page application using Backbone's router to modify browser history. I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.