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/