私がconsole.logに次のコードを書き込むと、コレクションの値が2倍になっていることがわかりました。たとえば、2つのレコードが保存されている場合、3つのレコードに対して4回フェッチされ、9回フェッチされます。これはゾンビのビューに関連していますか?私は彼らがthis.listenToの導入で修正されたと思った。データを複数回取得するバックボーン
render: function() {
console.log("inside render");
var self = this;
this.model_date = $("#date").val();
this.dateUrl = "https://xxxxx.firebaseio.com/" + this.model_date.replace(/\//g, '');
//create an instance of the collection constructor FoodCollection with the current date
this.foodCollection = new app.FoodCollection([], { url: this.dateUrl });
this.listenTo(this.foodCollection, 'add', this.render);
this.$list.html('');
//this part is executed multiple times based on the number of records saved.
this.foodCollection.each(function(food) {
console.log("iterating over foodCollection");
var view = new app.FoodRecords({ model: food });
self.$list.append(view.render().el);
});
},
//called when an item is added and the add-food button is clicked
addFood: function() {
console.log("inside addFood");
if (this.$input.val() == '') {
return;
};
this.foodCollection.create(this.newAttributes());
this.$input.val('');
},