我有一个 User 模型,它有一个 BankAccount 模型,我有一个 View ,谁的模型属性设置为 User 模型,在那个 View 的模板中,我引用了银行账户的一个属性:
<%= bankAccount.get('description') || 'No linked account' %>
如果我更新了 bankAccount, View 不会接受更改:
user.get('bankAccount').set('description', 'foobar')
我尝试在 User 模型的初始化中添加以下内容,但它也没有用。
initialize: function() {
var self = this;
this.on('change:bankAccount', function() {
self.get('bankAccount').bind('change', function() {
self.trigger('change');
});
});
}
有什么建议吗?此外,如果有帮助,这两种模型都使用主干关系。
最佳答案
让您的 View 订阅 BankAccount 模型上的 description
更改事件并重新呈现自身
UserView = Backbone.View.extend({
initialize: function() {
this.model.get('bankAccount').on('change:description', this.render, this);
}
});
关于javascript - 绑定(bind)子模型以在 Backbone /Marionette 中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872687/