If you look at the ToDo app in Dev tools, a new ToDoView is created within AppView everytime a new ToDo(model) is created. But I dont see a "collection" of ToViews. I am asking this because I'm thinking a view is destroyed when the model is destroyed, but cant find the reference. Thanks
1 Answers
Views generally are in-memory structures (like the DOM itself), so as long as your javascript code holds a reference to them (thus making them ineligible for garbage collection by the browser's JS engine), they continue to exist. Close the window and they're gone. Click a link to a different page that does a full-page load and they're gone.
However, the view doesn't automatically respond to a model being destroyed. You have to manually wire up something like this.listenTo(this.model, 'destroy', this.remove.bind(this)) or something along those lines if you want the model to disappear from the DOM when it is deleted via backbone.
Also, as you have noticed, it is common when you have a collection of models that you want rendered to just make a new view for each model, render it, and then don't keep a reference to it. When the collection changes, the outermost element gets emptied, new view instances get created and rendered, and thus the old view instances are eligible for automatic garbage collection. So javascript will handle cleaning up those old views but that will not have any effect on the corresponding models nor trigger any AJAX requests.