calling Backbone.history.navigate(...) leads to recursion?

52 views Asked by At

I am using bootstrap, backbone, marionette in my app with require js support. This is a moderately large application with many views and sub views. As it consists of tabbed display, I am using bootstrap tabs.

In my layout view, I am handling the tab shown event and trying to do tab pane specific rendering as below...

var AppLayoutView = Backbone.Marionette.LayoutView.extend({
    el : "body",
    regions: {
        headerRegion: "#ecp_header",
        bodyRegion: "#ecp_body",
        contentRegion: "#home"
    },

    events: {
        'shown.bs.tab ul.nav-tabs a': 'onTabShown'
    },

    ...

    onTabShown: function(e) {
        var self = this;
        console.log("App layout view: 'onTabShown' executing...");
        var tabId = $(e.currentTarget).attr('id');

        if (tabId === 'home-tab') {
            /**  Show the dashboard (home) view */
            require(['user_dashboard_layout'], 
                    function(UserDashboardLayoutView) {

            // update the URL in addressbar, so it can be available in history stack     
            Backbone.history.navigate('dashboard');

            var dbLytView = new UserDashboardLayoutView();
            dbLytView.render();
            //self.bodyRegion.show(dbLytView);
            //self.contentRegion.attachView(dbLytView);
            });
        } else if (tabId == "scheduling-tab") { ... }

All of this was working decently, until I added the line the history navigation line as shown in the above code.

Backbone.history.navigate('dashboard');

I also added app_controller to take care of routing. But after this, I observe a strange behavior. I see that the above fn "onTabShown" gets invoked multiple times, as seen in the browser console window (screenshot attached), whenever I perform login/logout on my app. enter image description here

BTW, in my SPA (single page app), when user logs in, I show dashboard (if the user is logged in), or show welcome page (if not logged in).

If the offending line (history.navigate(...)) is present, I can see that tabShown is invoked multiple times, that is, for each login/logout it gets accumulated (some sort of strange recursion, or stack is not unwound).

But, if I comment out the history.navigate line, it doesn't perform page refresh after logout.

My basic question is...

  • "does the backbone.history.navigate(...) fn play any role in actual page navigation/refresh, apart from just updating the history stack?

From the documentation, it appeared that we need to call bb.h.navigate(...) just to keep the url in addressbar in sync with our current state of app. However, I am experiencing this strange behavior?

Since the app is a bit fairly complex, I may not have provided all relevant details for soliciting a proper answer to this qn.

May someone point me in right direction...?

0

There are 0 answers