2

I've been having some difficulty understanding exactly what happens when a model is manipulated in Backbone.

1) When calling #get on a Collection to "grab" a model, is the model the same model as the Collection's model? (e.g., updating the model will update the Collection's model)

2) If a model is added to various collections, do all of those collections contain the actual model (or a "copy" of the model)? It seems to me that it's the "copy" because when I try to destroy a model that has been added to various collections, not all the models in the various collections are destroyed.

Thanks! Appreciate any insights.

fibono
  • 773
  • 2
  • 10
  • 23

1 Answers1

0

Like every other object in Javascript, Backbone objects are 'passed by a copy of the reference'. The best way to think about this is that javascript has a piece of data in memory, and variables are nothing more than pointers to those bits of data. When you set one variable equal to another, what you really get is two copies of the pointer, both pointing at the same piece of data in memory. So, applying this to your question:

  1. Yes. When you 'get' the model, what you 'get' from backbone is a pointer to the place in memory where the object is stored. Now you have two pointers (one in the collection and one in your variable), and you can perform operations on either of them and either will perform that operation on the same piece of data in memory.
  2. Kind of. Each collection has, again, a pointer to the same object/model. When you remove that pointer from a collection, the other pointers remain pointing to the same piece of memory, and that memory is not erased, because it's still being pointed at from other collections. model.destroy() will trigger a destroy event on both the model and the collection it is holding a pointer to in it's collection attribute. However, the model can not hold pointers to multiple collections if it is a part of more than one collection. So on your destroy event, it is removed only from the collection it was last assigned to - the one it holds in the model.collection attribute. Ordinarily, when no variables are holding pointers to the piece of data in memory, that memory will be erased, however, in this case, because your other collections have pointers to the model, the model remains in memory as a part of those collections.
Community
  • 1
  • 1
Michael.Lumley
  • 2,345
  • 2
  • 31
  • 53
  • Thank you, very helpful. How did you learn all of this? Is there one good resource out there you would recommend? – fibono May 07 '15 at 05:55
  • Well, there isn't really any one killer resource that would address this specific question. The basic JavaScript 'pass-by-copy-of-reference' knowledge is something I picked up after months of reading Stack Overflow. But when it comes to understanding specifically how Backbone works, there's no substitute for a thorough examination of the [documentation](http://backbonejs.org/) and the [annotated source](http://backbonejs.org/docs/backbone.html). – Michael.Lumley May 07 '15 at 10:10
  • Also, @fibono, if my answer was helpful, you should consider marking it as the accepted answer, and/or upvoting it. ;) – Michael.Lumley May 08 '15 at 18:39
  • Will do. Unfortunately, I'm new to stackoverflow, so I'll upvote you when I have enough reputation! – fibono May 10 '15 at 21:40
  • You can always mark an answer as accepted by clicking the check mark. It does not require any specific amount of reputation. – Michael.Lumley May 10 '15 at 22:40
  • Also, you will get two reputation for accepting an answer - which will give you enough to upvote as well. ;) – Michael.Lumley May 10 '15 at 22:41