Backbone.js路由条件默认路由

时间:2013-02-14 10:40:15

标签: javascript backbone.js underscore.js

我有一个带有许多不同选项的路由器,对于一个实例,我需要加载其中一个路由,这很有效,但是总是调用的默认路由会加载一些额外的模板。
只是想知道如果在这种情况下有一种方法可以说这条路线是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
    };
});

如果我没有详细解释,请向我询问更多信息,期待一些帮助,我对骨干很新。

1 个答案:

答案 0 :(得分:0)

您需要将默认操作移至路线词典的底部,以便在所有其他路线都失败时不会先匹配,而是作为最后的手段。

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