2013-04-21 101 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