2013-02-14 155 views
1

我有一个路由器与一些不同的选项,我需要加载其中一个路由,这很好,但总是调用默认路由加载一些额外的模板。
只是想知道是否有方法来说明在这种情况下如果此路由为CMS,则不要从默认路由加载元素。从我的路由器
代码:Backbone.js路由条件默认路由

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 
    }; 
}); 

如果我没有足够的详细说明,请跟我要更多的信息,期待一些帮助,我非常新的骨干。

回答

0

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

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