2013-04-21 146 views
0

配合,我試着從子視圖訪問骨幹視圖的屬性(不知道是否是正確的面額)。 總之,這裏的一些代碼:訪問父視圖的屬性

App.Views.ViewEditGuest = Backbone.View.extend({ 
    el: '#main-content .container_12', 
    totalPayments: 0, 
    totalBought: 0, 

    template: _.template(App.Templates.ViewEditGuest), 

    events: { 
     'click #sell' : 'sell' 
    }, 

    sell: Backbone.View.extend({ 
     el: '#consumos', 
     template: _.template(App.Templates.VEGConsumos), 

     render: function(){ 
       // I need to acces ViewEditGuest's totalPayments and 
       // totalBought 
       this.$el.html(this.template()); 
       return this; 
      } 
    }), 

    render: function(){ 
      this.$el.html(this.template()); 
      return this; 
    } 
}); 

所以,我需要從銷售的渲染方法來訪問ViewEditGuest的totalPayments和totalBought。 我一直在尋找周圍,但找不到任何解決方案。

任何想法?

謝謝!

回答

2

正確方法是將數據存儲在模型中,然後在兩個視圖中更新屬性時監聽事件。

,但回答儘可能少的變化的代碼儘可能的問題,

sell: Backbone.View.extend({ 
    parentView: this, 
    el: '#consumos', 
    template: _.template(App.Templates.VEGConsumos), 
    render: function(){ 
     console.log(options.parentView.totalBought); 
     this.$el.html(this.template()); 
     return this; 
     } 
+0

我已經試過了,但是還是不行。 它返回給我的選項是未定義的。我試過this.parentView,但它返回我所有的窗口對象。 任何想法爲什麼?謝謝! – Pablo 2013-04-21 21:11:08

+0

任何想法爲什麼?因爲'this'是定義類時的上下文,也就是'window'對象(我會假設),它會存儲在原型中。它不會是父視圖。 – Loamhoof 2013-04-21 21:15:56

+0

.bind()相應的方法,或將其複製到一個變量。 – krs 2013-04-21 22:28:08