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

  • SEARCH
  • Home
  • 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 8467239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:35:06+00:00 2026-06-10T15:35:06+00:00

I got following router.js: define([‘jquery’, ‘underscore’, ‘backbone’, ‘views/settings/index’], function($, _, Backbone, SettingsView) { var

  • 0

I got following router.js:

define(['jquery', 'underscore', 'backbone', 'views/settings/index'], function($, _, Backbone, SettingsView) {

    var Router = Backbone.Router.extend({

        container: $('div.main'),

        routes: {
                "settings": "settings"
                , "settings/:query": "settings"
        },

        settings: function(query) {
            this.container.html(SettingsView.render().el);
        }

    });

    if (Router._instance) return Router._instance;

    Router._instance = new Router();

    return Router._instance;

});

The router gets called in the main.js filed which is marked as first dependency in the requirejs config:

define(['jquery', 'underscore', 'backbone', 'router'], function($, _, Backbone, router) {

    // ajax settings (sent cors cookies)
    $.ajaxSetup({ xhrFields: { withCredentials: true } });

    // register master router
    window.router = router;

    // start backbone history
    Backbone.history.start({ root: "/" });          

});

In some view I now want to do:

define(['jquery', 'underscore', 'backbone', 'router'], function($, _, Backbone, router) {

    var View = Backbone.View.extend({

        initialize: function() {
            router.on('route:settings', this.change, this);
        },

        change: function(query) {
            // load some new content for example
        }

    });

    return View;

});

Than I am getting this error message:

Uncaught TypeError: Cannot call method 'on' of undefined 

UPDATE:
I added a console.log to each module. So I know in which order the modules are executed. I mentioned that requirejs has the wrong order:

view.js -> main.js -> router.js

instead of:

router.js -> main.js -> view.js

Let see how I can solve this

UPDATE2:
I now wrapped the router event binding part into a setTimeout loop:

        setTimeout(function() {
            window.router.on('route:settings', this.select, this);        
        }.bind(this), 20);

And see there it works!

This truly just is a fix but nevertheless a fix 🙂

  • 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-10T15:35:07+00:00Added an answer on June 10, 2026 at 3:35 pm

    Why are you trying to create a singleton instance on your router?

    Requirejs resolves every module only once as far as I understand and
    as far as I would expect from any decent module loader.

    #router.js
    define(['jquery', 'underscore', 'backbone', 'views/settings/index'], 
      function($, _, Backbone, SettingsView) {
      //your code omitted ...
    
      //no need to do this
      //if (Router._instance) return Router._instance;
      //Router._instance = new Router();
      //return Router._instance;
    
      //this should work as well and give you a "singleton" instance
      return new Router();
    

    });

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

Sidebar

Related Questions

I've got the following routes.php configuration Router::connect('/:type/:slug;:id', array( 'controller' => 'content', 'action' => 'show',
I've got the following routes: routes.IgnoreRoute({resource}.axd/{*pathInfo}); routes.MapRoute(null,Conference/{shortName}/Submission/{submission}/{action}, new { controller = Conference, action =
I got following repeating syntax and I want to keep all <h1> that is
I got following code in an XHTML-document and every browser saysit is malformed :(
I got following error... System.NullReferenceException: Object reference not set to an instance of an
I got following code from net and it looks everything is proper but i'm
I've got following code in my AsyncTask. The only thing I want the AsyncTask
I've got following method in a class, which is a custom adapter for an
I've got following bash script to do something for each parameter of the script
I've got following problem: (c#) There is some class (IRC bot), which has method,

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.