2014-10-16 119 views
0

我試圖讓我的路由器決定顯示一個登錄只有家庭觀點,否則登錄視圖或registerview。無法路由邏輯我的意見從骨幹路由器

我的應用程序在最初的localhost:8080/indexapp。

我加載它在數據主文件。但是我認爲,並沒有觸發做出這一決定的行動與路線相關。

是否有可能在加載的頁面上有一個路由器來觸發路由。

另外,我有一條註釋爲this.loginModel.fetch();的行,它導致undefined不是函數錯誤。

define([ 
    'jquery', 
    'ratchet', 
    'underscore', 
    'backbone', 

    'login/loginview', 
    'login/loginmodel', 
    'register/registerview', 

    'home/homeview', 
    ], 
    function($, Ratchet, _, Backbone, LoginModel, LoginView, RegisterView, HomeView){ 
     var MainRouter = Backbone.Router.extend({ 
      routes: { 
       "": "showHome", 
       //"/": "showHome", 

       "login": "showLogin", 
       "register": "showRegister" 
      }, 
      initialize: function(){ 
       Backbone.history.navigate("home", {trigger:true}) 
      }, 
      showHome: function(){ 
       //var self = this, loginModel = new LoginModel(); 
       this.loginModel = new LoginModel(); 
       //this.loginModel.fetch(); 

       if(this.loginModel.get('loginp') == true){ 
        this.homeView = new HomeView(); 
        this.homeView.render(); 
       } 
       else { 
        Backbone.history.navigate("/login", {trigger:true}) 
       } 
       /* 
       this.loginModel.fetch({ 
        success: function(model){ 
         if(model.get('loginp') == true){ //show you app view for logged in users! 
          this.homeView = new HomeView(); 
          this.homeView.render(); 
         } 
         else { //not logged in! 
          Backbone.history.navigate("/login", {trigger:true}) 
         } 
        }, 
       }); 
       */ 
      }, 
      showLogin: function(){ //display your login view 
       this.loginView = new LoginView; 
       //this.loginView.fetch(); 
       this.loginView.render(); 

      }, 
      showRegister: function(){ //display your register view 
       this.registerView = new RegisterView; 
       //this.registerView.fetch(); 

       this.registerView.render(); 
      }, 
     }); 

     return MainRouter; 
}); 

loginmodel:

define([ 
    'underscore', 
    'backbone', 
    ], 
    function(_, Backbone) { 
     var LoginModel = Backbone.Model.extend({ 

      urlRoot: '/login', 
      initialize: function(){ 
       this.fetch(); 
      }, 
      defaults: { 
       username: null, 
      }, 

     }); 

     return LoginModel; 
}); 

loginview:

define([ 
    'jquery', 
    'ratchet', 
    'underscore', 
    'backbone', 

    'login/loginmodel', 
    'text!login/logintemplate.html', 

    ], 
    function($, Ratchet, _, Backbone, LoginModel, LoginTemplate){ 
     var LoginView = Backbone.View.extend({ 

      el: $('body'), 

      model: new LoginModel, 
      /* 
      initialize: function(){ 
       this.model = new LoginModel; 
      }, 
      */ 
      template: _.template(LoginTemplate), 

      render: function(){ //display your login view 
       this.$el.html(template(this.model.attributes)); 
      }, 

     }); 

     return LoginView; 
}); 
+0

上面的問題是我的,我正在考慮刪除它。因爲這個問題起源於loginview依賴性的錯誤順序。我應該刪除它嗎?或者回答。 – 2014-10-16 14:50:04

回答

0

很高興見到你按照我的想法! ;)

你唯一缺少的是在$(document).ready中使用Backbone.history.start()啓動路由。

http://backbonejs.org/#Router

它的工作原理是這樣的:你需要在調用之前Backbone.history.start實例路由器(S)()。

因此,在您的主應用程序文件中,需要您的MainRouter,創建一個實例並啓動歷史記錄。

$(document).ready(function(){ 
    new MainRouter(); 
    Backbone.history.start(); 
}); 
+0

謝謝查理:)我會遵循它,你的這個答案提醒我我已經用我的包裝測試了你的代碼,它的工作原理,所以我應該打勾你的答案。現在我開始在我的頂層文件,我的數據主。 '需要([ '主/ mainrouter'],功能(MainRouter){ \t VAR mainRouter =新MainRouter; \t Backbone.history.start(); });' 但是我開始歷史上它沒有工作。問題是loginview文件中的依賴加載錯誤。我應該刪除這個問題嗎? – 2014-10-16 14:52:28

+0

是的,但包裝在$ document.ready中,以防萬一還沒有完成加載! – CharlieBrown 2014-10-16 14:54:39

+0

好的,謝謝。我回來的時候會嘗試。 – 2014-10-16 14:56:33