nizzle
nizzle

Reputation: 1046

Backbone keeps creating new models

Everytime I do a "fetch" of my collection, backbone creates new models for every item. The old models stick around in memory, causing a big memory leak.

There are no changes of the data between "fetch" calls, should backbone not recognize that there are no changes and carry on?

Upvotes: 0

Views: 78

Answers (2)

nizzle
nizzle

Reputation: 1046

The problem is different than I thought.

I keep a list of sub-view in my view so I can remove them. However, there appear to be 2 different properties with the same name. One keeping a hold of the views. enter image description here

Upvotes: 0

Chickenrice
Chickenrice

Reputation: 5727

Backbone collection will use "set" method to update model data in collection. Set is a smart method and it will perform these checks when collection fetch data from server:

  1. Add: If a model in the list isn't yet in the collection it will be added.
  2. merge: if the model is already in the collection its attributes will be merged
  3. remove: if the collection contains any models that aren't present in the list, they'll be removed

Your problem is the collection will continuously add new model when you fetch data from server even though you just try to refresh data which is updated. I think your data don't have unique "id" attribute. So the collection can't perform "smart update" when it fetch data from server.

{id:"1234",name:"blabla","tel:0600000000"}

I hope this is helpful for you.

Upvotes: 2

Related Questions