1

我有一个带有许多不同选项的路由器,例如,我需要加载其中一个运行良好的路由,但是始终称为默认路由的默认路由会加载到一些其他模板中。
只是想知道是否有办法说如果在这种情况下这条路线 CMS,那么不要从默认路线加载元素。
来自我的路由器的代码:

define([
  'jquery',
  'underscore',
  'backbone',
  'views/common/header',
  'views/common/menu',
  'views/rightPanel',
  'views/landing',
  'views/personalBanking',
  'views/currentAccounts',
  'views/signIn',
  'views/cms/cmsView',
], function($, _, Backbone, headerView, menuView, rightPanelView, landingView, personalBankingView, accountsView, signInView, CMS){

    var AppRouter = Backbone.Router.extend({
        currentView: null,

        initialize: function() {
            this.showView(headerView);
            this.showView(menuView);
            //TODO set for TV-width
            if($(window).width() > 180) {
                this.showView(rightPanelView);
            }
        },

        routes: {
            '': 'defaultAction',
            'personal': 'showPersonalBankingView',
            'accounts': 'showCurrentAccountsView',
            'signIn': 'showSignInView',
            'CMS': 'CMSView',
        },



        defaultAction: function(actions){
            this.showView(landingView);
        },

        showPersonalBankingView: function(actions){
            this.showView(personalBankingView);
        },

        showCurrentAccountsView: function(actions){
            this.showView(accountsView);
        },

        showSignInView: function(actions){
            this.showView(signInView);
        },

        CMSView: function(actions){
            this.showView(CMS);
            console.log("cms view going on");
        },

        showView: function(view) {
            view.render();
            if(view.postRender) {
                view.postRender();
            }   
        }
    });

    var initialize = function(){
        new AppRouter();
        Backbone.history.start();
    };

    return {
        initialize: initialize
    };
});

如果我解释得不够详细,请向我询问更多信息,期待一些帮助我对骨干很陌生。

4

1 回答 1

0

您需要将默认操作移动到路由字典的底部,以便它不会首先匹配,而是在所有其他路由失败时作为最后的手段。

routes: {
    'personal': 'showPersonalBankingView',
    'accounts': 'showCurrentAccountsView',
    'signIn': 'showSignInView',
    'CMS': 'CMSView',
    '': 'defaultAction'
},
于 2013-03-04T07:30:46.440 回答