2012-07-11 40 views
0
movieDb.Router = Backbone.Router.extend({ 
     routes:{ 
      '':   'landPage', 
      'home':  'landPage', 
      'login': 'login', 
      'signup': 'signup' 
     }, 

     landPage: function(p){ 
      $('div#homepage').empty(); 
     }, 

     login: function(p){ 
      new loginViews(); // Calls its own model     
     }, 

     signup: function(p){ 
      new signupViews(); // Calls its own model 
     } 
    }); 

的問題是,從註冊來和呼叫登錄時,還調用以前的模型請求(註冊)導致登錄並在同一時間加入申請(我的模型是阿賈克斯請求),我如何在每次在視圖中創建新請求時刪除先前調用的模型。如何清潔視圖和模型在Backbonejs

謝謝。

回答

1

我發現的最簡單的方法是使用Backbone.Marionette https://github.com/derickbailey/backbone.marionette 它允許您使用單獨的區域用於單獨的功能。
如果你不想有其他扶養您可以手動做到這一點: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

,但如果你看到一個取上一個模型調用獲取另一個那麼你很可能無論是在名稱的碰撞,或者以某種方式引發第二次提取的事件鏈接。

+0

但在這種情況下,我不能把沒有通話的具體意見(loginViews,signupViews)因此,去掉模型/清潔的觀點實際上將阻止打電話給那個觀點的模型,我已經檢查了你給我的最後一個鏈接,對於像我這樣的初學者來說,我覺得它有點複雜。 – Barry 2012-07-11 05:37:25

+0

爲什麼不是 註冊:function(p){ window.signupModel = new SignupModel();新的signupViews({model:window.signupModel}); } – MarkKGreenway 2012-07-11 05:43:30

+0

這與我的實現沒有什麼不同,只是我在視圖中不是從路由器調用模型,所以它不能解決它,謝謝 – Barry 2012-07-11 05:53:31

0

我想和大家分享我是如何解決這個問題。(很基本的方法,不知道這是最好的做法,但它的工作)

我擴展了關閉功能的觀點

Backbone.View.prototype.close = function() { 
     this.unbind(); 
     this.undelegateEvents(); 
    }; 

並且在我的視圖中每次啓動事件時都會在我的路由器中創建一個showView函數。

showView: function (view) { 
     //destroy current view 
     if(this.currentView !== undefined) { 
      this.currentView.close(); 
     } 

     //create new view 
     this.currentView = view; 
     this.currentView.delegateEvents(); 

     return this.currentView; 
    } 

然後調用showView功能在我看來

 addMovie: function(p){ 
     this.showView(this.movieForm('add'));    
    }