听一个集合添加/更改作为视图的模型属性 [英] listen to a collection add/change as a model attribute of a view

查看:210
本文介绍了听一个集合添加/更改作为视图的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测量视图,它有一个相关联的计量模型,其中有两个集合,再presentationsCollection和BeatsCollection。测量查看嵌套子重新presentation意见,每个都有自己的再presentation模型,并全部重新presentation车型分享到测量视图的BeatsCollection的引用相同的属性。

I have a Measure View, and it has an associated Measure Model, which has two collections, RepresentationsCollection and BeatsCollection. The Measure View has nested child Representation views, each with their own representation model, and all representation models share the same attribute of a reference to the Measure View's BeatsCollection.

我知道,如果你正在听一变更事件,收集不会补上一时作出回应。你应该使用绑定。该文档是不是最好的。所以,如果孩子再presentation查看看起来是这样的:

I know that if you are listening to a change event, the collection won't respond when something is added. You are supposed to use bind. The docs aren't the best. So if the child Representation View looks like this:

representation.View = Backbone.View.extend({
  initialize: function(options) {
    // this.beatsCollection is a reference to the Parent Measure View attribute beatsCollection which is a collection
    this.model.listenTo(this, 'change:beatsCollection', this.render);
    this.model.on('change:beatsCollection', this.render, this);
    this.bind.listenTo(this, 'change:beatsCollection', this.render);
  },

和这里的模型:

representation.Model = Backbone.Model.extend({
  initialize: function(options) {
    console.log(options); 
    this.idAttribute = options.idAttribute;
    this.type = options.type;
    this.beatsCollection = options.beatsCollection;
 } 

我如何侦听关联的模型这一观点的属性,它有一个属性,链接到一个集合的另一种模式?

How do I listen for the attribute on this views associated model, which has an attribute, that is linked to a collection on another model?

下面是一个 Plnkr:http://plnkr.co/edit/z4mWqo1v0nDe13TiB9r3? p = preVIEW 结果
首先点击添加重presentation'。结果
其次,点击添加一拍,并注意节拍数不更新。
第三,如果你点击'添加一个重新presentation再次,它会增加另外一个是正确数量节拍。
中联重presentation.js查看,我们怎么得到回复presentation意见重新渲染,当任何同级的意见单击添加一个节拍

Here is a Plnkr: http://plnkr.co/edit/z4mWqo1v0nDe13TiB9r3?p=preview
First click 'add a representation'.
Second, click 'add a beat' and notice that the number of beats don't update. Third, if you click 'add a representation' again, it will add another one with the correct number of beats. In the Representation.js View, how do we get the Representation Views to re-render, when ANY of the sibling views click 'add a beat'

推荐答案

更​​新Plnkr: http://plnkr.co/edit/JtyxnhaGlPVijhbRPt5v?p=$p$pview

updated Plnkr: http://plnkr.co/edit/JtyxnhaGlPVijhbRPt5v?p=preview

诀窍是在这里重新presentation.View替换

trick was here in representation.View replacing

initialize: function(options) {
  if(options.model){this.model=options.model;}
  this.model.listenTo(this, 'change:beatsCollection', this.render);
  this.model.on('change:beatsCollection', this.render, this);
  this.model.on('change:beatsCollection', this.render, this);
  //this.model.on('destroy', this.remove, this);
},

initialize: function(options) {
  if(options.model){this.model=options.model;}
  this.listenTo(this.model.beatsCollection,  'add remove reset', this.render);
},

这篇关于听一个集合添加/更改作为视图的模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆