0

私は2 つのコレクション、コレクション、および[resentation] コレクションmeasureで構成されているモデルを持っています。各コレクションは、それぞれモデルとモデルで構成されています。beatsmeasureRepbeatrepresentation

measureRep[resentation] コレクションが (representationモデルの追加または削除によって)変更されるたびに、measureView(モデルを持っているmeasureため、measureRep[resentation] コレクションがある) を render function() を使用して再レンダリングする必要があります。

次の関数を使用して、別のビューに新しいモデルを追加しています。

var representationModel = new RepresentationModel({representationType: newRepType});
StageCollection.get(cid).get('measures').models[0].get('measureRepresentations').add(representationModel);

追加の前後にmeasureとそのmeasureRepコレクションが正しく追加されていることがわかりますが、 のバインド呼び出しmeasureViewは変更を登録せず、レンダー関数を呼び出していません。バックエンドが更新されていることを示すために、モデルにバインドを設定しましたが、応答しません。これにより、ビューとモデルが分離されていると思われますが、最初はモデルからレンダリングされるため、意味がありません。関連ファイルは次のとおりです。

measureView.js [ビュー]:

define([...], function(...){
  return Backbone.View.extend({

    initialize: function(options){
      if (options) {
        for (var key in options) {
          this[key] = options[key];
        }
        this.el = '#measure-container-'+options.parent.cid;
      }

      window.css = this.collectionOfRepresentations; //I use these to attach it to the window to verify that the are getting updated correctly
      window.csd = this.model;  // Same
      _.bindAll(this, 'render'); // I have optionally included this and it hasn't helped
      this.collectionOfRepresentations.bind('change', _.bind(this.render, this));

      this.render();
    },

    render: function(){
      // Make a template for the measure and append the MeasureTemplate
      var measureTemplateParameters = { ... };
      var compiledMeasureTemplate = _.template( MeasureTemplate, measureTemplateParameters );
      // If we are adding a rep, clear the current reps, then add the template
      $(this.el).html('');

      $(this.el).append( compiledMeasureTemplate )

      // for each rep in the measuresCollection
      _.each(this.collectionOfRepresentations.models, function(rep, repIndex) {
        var measureRepViewParamaters = { ... };
        new MeasureRepView(measureRepViewParamaters);
      }, this);

      return this;
    },
    ...
  });
});

measure.js [モデル]:

define([ ... ], function(_, Backbone, BeatsCollection, RepresentationsCollection) {
  var MeasureModel = Backbone.Model.extend({
    defaults: {
      beats: BeatsCollection,
      measureRepresentations: RepresentationsCollection
    },
    initialize: function(){
      var logg = function() { console.log('changed'); };
      this.measureRepresentations.bind('change', logg);
      this.bind('change', logg);
    }
  });
  return MeasureModel;
});

Representations.js [コレクション]:

define([ ... ], function($, _, Backbone, RepresentationModel){
  var RepresentationsCollection = Backbone.Collection.extend({
    model: RepresentationModel,
    initialize: function(){
    }
  });

  return RepresentationsCollection;
});

measureまた、子コレクションではなくモデルにバインドを登録しようとしましたが、どちらも機能しません。

_.bindAll(this, 'render');
this.model.bind('change', _.bind(this.render, this));
4

1 に答える 1

1

参照: https://stackoverflow.com/a/8175141/1449799

コレクションへのモデルの追加を検出するには、add イベントをリッスンする必要があります (コレクション内のモデルが変更されたときに発生する change イベントではありませんhttp://documentcloud.github.io/backbone/#Events -カタログ)。

だから試してください:

this.measureRepresentations.bind('add', logg);
于 2013-08-23T16:03:25.677 に答える