Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SubRoute causes routes to get evaluated multiple times #19

Closed
rweng opened this issue Jan 22, 2013 · 2 comments
Closed

SubRoute causes routes to get evaluated multiple times #19

rweng opened this issue Jan 22, 2013 · 2 comments

Comments

@rweng
Copy link

rweng commented Jan 22, 2013

So I have this MainRouter.js:

var Router = Backbone.Router.extend({
            routes: {
                '': "root",
                users: "users",
                test: 'test',
                '*actions': 'defaultRoute'
            },

            test: function () {
                debugger;
            },

             createSubRouters: function () {
                require(['routers/CompanyRouter'], function (Router) {
                    new Router('companies', {createTrailingSlashRoutes: true});
                });
                 ...
             }

The router is loaded in an Marionette App, like this:
app.js:

           app.addInitializer(function () {
                // we require the router here to avoid circular dependencies
                require(['routers/MainRouter'], function (Router) {
                    app.router = new Router();
                    Backbone.history.start();
                    app.router.createSubRouters();
                })
            });

When I go on #/test, the debugger is triggered when starting the history in app.js.
However, when the subrouters are created, Backbone.history.loadUrl (https://github.com/ModelN/backbone.subroute/blob/master/backbone.subroute.js#L81) causes the function to be executed again.

This means, that when I first time load my app, the controller/router function is executed (1 + numberOfSubRouters) times.

Can I avoid this somehow? I tried to move the app.router.createSubRouters(); before the Backbone.history.start(), but with no success.

Any help is greatly appreciated :)

@geekdave
Copy link
Contributor

Hi Robin,

I just pushed v0.3.2 which should fix this issue. Basically that call to loadUrl should only have been invoked if the browser url hash actually matches the subroute being initialized. I didn't run into this myself because I am lazy-loading my sub-routes, only when they are first navigated to.

Please let me know if this fixes the issue for you.

Thanks,
Dave

@rweng
Copy link
Author

rweng commented Jan 23, 2013

@geekdave Jep, it does. Thank you!

@rweng rweng closed this as completed Jan 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants