访问父视图的属性

时间:2013-04-21 20:34:36

标签: javascript backbone.js backbone-views

Mates,我试图从子视图访问骨干视图的属性(不知道是否是正确的面额)。 无论如何,这里有一些代码:

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;
    }
});

因此,我需要从sell的render方法访问ViewEditGuest的totalPayments和totalBought。 我一直在寻找周围,但找不到任何解决方案。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分: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;
      }