javascript - 访问父 View 的属性

标签 javascript backbone.js backbone-views

friend 们,我正在尝试从 subview 访问主干 View 的属性(不知道是否是正确的名称)。 无论如何,这是一些代码:

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 的渲染方法访问 ViewEditGuest 的totalPayments 和totalBought。 我一直在四处寻找但找不到任何解决方案。

有什么想法吗?

谢谢!

最佳答案

正确的方法是将数据存储在模型中,然后在两个 View 中更新属性时监听事件。

但要在尽可能少地更改代码的情况下回答问题,

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

关于javascript - 访问父 View 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135926/

相关文章:

jquery - 是否可以停止 Backbone "read"请求

javascript - c3JS加载函数

javascript - 显示带有流体设计的内联 block 未对齐同一行

javascript - 从嵌套属性中提取

javascript - 如何将 Backbone 模型序列化为 URL

javascript - 为什么我的 jQuery 隐藏事件没有触发并且我的 Backbone subview 没有渲染?

javascript - 主干更改事件未触发

javascript - 在 Node 中强制进行垃圾收集以测试 WeakRef 和 FinalizationRegistry

javascript - JavaScript 中的装饰器对象

javascript - 使用 _.template 将 JSON 字符串转换为下划线模板