1

我需要顯示three different views這些與three different model或集合相關。
爲了執行這個任務我寫了下面的代碼。 (*)
請告訴我,如果這是正確的做法,無論如何它的工作原理。Backbone.View:刪除與不同型號相關的不同視圖

這裏我的問題。
在這種觀點之一中,我們假設firstView有可能對服務器執行DELETE request,該服務器會注意刪除與此three view相關的所有數據。

現在我需要刪除我的三個視圖... 但從firstView我無法訪問其他兩個視圖。

1)我該如何執行此任務?
2)我應該重新設計/改進我的實施嗎?


(*)

// module for display three different views 

define([ 
    "js/views/01View", 
    "js/views/02View", 
    "js/views/03View" 
], function (FirstView, SecondView, ThirdView) { 

    var MainView = Backbone.View.extend({ 

     initialize: function() 
     { 
      this.render(); 
     }, 

     render: function() 
     { 
      var movie_id = this.options.movie_id; 
      this.firstView = new FirstView(movie_id); 
      this.secondView = new SecondView(movie_id); 
      this.thirdView = new ThirdView(movie_id); 
     } 
    });  

    return MainView; 
}); 

PS:

的_id用於建立集合或模型

url1: http://localhost/movie/movie_id (model1) 
url2: http://localhost/movie/movie_id/followers (collection2) 
ulrs: http://localhost/movie/movie_id/feeds (collection3) 

當我刪除的URL參數model1與coll有關的view2和view3 ection2和collection3應該被刪除。

+0

我對您的評論困惑,你說,你有三種不同的觀點和三個不同的模型,但你傳遞相同的model_id到你的意見。 –

+0

model_id(同一個id)用於對服務器執行三種不同的提取,這對應於三種不同的數據模型。我將在我的問題上添加更多信息。 – underscore666

+0

感覺就像是在圍繞視圖構建數據而不是對數據的看法。看看下面的Backbone插件:https://github.com/PaulUithol/Backbone-relational/,並正確地建立你的數據之間的關係。 –

回答

1

爲了根據我們的評論對話來解決您的問題,骨幹架構使用事件,所以爲什麼不使用事件聚合器來發送事件,不要限制自己的主幹結構。 fire an event from one view to another in backbone此模式爲您的問題提供了一個優雅的解決方案。

+0

感謝您的回答。 – underscore666

1

Views不應對直接方法調用做出響應,而是響應事件。說你可以創建一個共同EventAggregator入店從各個視圖(如@ 20100在他的回答解釋了),或者你通過連接一個常見型號的意見,並讓每個視圖其自身更有趣事件的它。

在你的情況,你可以實例電影模式出來的意見實例化並連接周圍的三個視圖:

// code simplified and not tested 
var MainView = Backbone.View.extend({ 
    initialize: function (opts) { 
    this.movie = new Movie({ id: this.opts.movie_id}) 
    this.movie.fetch(); 
    this.render(); 
    }, 

    render: function() { 
    this.firstView = new FirstView(this.movie); 
    this.secondView = new SecondView(this.movie); 
    this.thirdView = new ThirdView(this.movie); 
    } 
}); 

var ThirdView = Backbone.View.extend({ 
    initialize: function(opts) { 
    this.movie = opts.movie; 
    this.movie.on("destroy", this.cleanUp, this) 
    this.followers = // fetch the followers as you do now, use this.model.id 
    } 

    cleanUp: function(){ 
    // your clean up code when model is detroyed 
    } 
}); 
+0

我喜歡'EventAggregator'的想法,但是在你的代碼中,我看不到我必須放的地方。我想在'MainView'中,然後將它作爲參數傳遞給其他視圖。這樣對嗎? – underscore666

+0

我沒有解釋'EventAggregator'的想法,@ 20100就是這樣做的。但是,如果非常相似,您可以使用@ 20100的答案中的鏈接,而不是使用通用的'Model',而是使用通用的'EventAggregator'。 – fguillen

相關問題