私は2 つのコレクション、コレクション、および[resentation] コレクションmeasure
で構成されているモデルを持っています。各コレクションは、それぞれモデルとモデルで構成されています。beats
measureRep
beat
representation
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));