2

I am building a SPA and am trying to figure out a safe and clean way to delete all backbone entities when navigating away from one section of the application. I am aware of the Model.destroy(), View.remove() and Collection.reset() methods. My main concerns are:

  • Model.destroy() takes care of destroying the model on the server. Does one still need to manually delete the Javascript model?
  • Similariy, I realize that View.remove() will remove the view from the DOM. How should one safely get rid of the view object?
  • Collection.reset() clears the models in the collection. Will this also delete the underlying Javascript model objects or do they need to be explicitly deleted?
  • How would one get rid of the collection object itself?

My questions may seem simple to some but they have been confusing me for a while. I have not found any useful information regarding this exact problem which is why I decided to post here. I am also a relatively new to Javascript and am unaware of Javascript's garbage collection scheme. Does Javascript have a garbage collector and will it take care of deleting all such objects?

I am also looking into understanding the best way to remove Views.

  • View.remove() only removes the view from the DOM. My guess is that one still needs to take care of removing/destroying the underlying Model and unbinding all event listeners. Is that correct?
  • I often use _.bindAll to change the context of a function call. I have not come across a way to unbind this binding. My understanding says that this is unnecessary. Am I right?

2 Answers2

1

Here is a good resource for learning about garbage collection in javascript:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

To summarize, the only thing you need to worry about with Backbone is removing custom event handlers in your view, and a common way to do this is to override remove and remove the handlers before calling Backbone.View.prototype.remove. You don't need to manually remove events that you declared in the events property of the view, if you are using that.

To be a little more specific, lets consider the three types of objects you are asking about in turn. If you are using the backbone router, then you are probably creating a view object in a router method, maybe assigning it to a var, and then setting the html somewhere on the page. When the user navigates away, a different router method is invoked, and the reference you created to this view is unreachable. It will get garbage collected assuming you didn't create it as a property on window or your root level object or something like that.

The remove method is about removing the view from the DOM. The garbage collector takes care of removing the view from memory. The remove method is a convenient place to put any cleanup code that you need to run before taking the view off the page, so unhook your custom event handlers here.

Likewise, model.destroy is not about destroying the model object stored in memory, its about sending an AJAX DELETE request to the server. Model objects are garbage collected like everything else, and will go away once they are unreachable. If the only references to a model are contained within a view, then removing that view will cause the model to be garbage collected. Same goes for collections.

And for your last point, the underscore bind will not be an issue. Its about binding context (the value of this that a function is invoked with), not about binding event handlers. Remove those event handlers as usual.

wrshawn
  • 429
  • 2
  • 6
  • Thank you for your answer. Its good to learn about garbage collection in Javascript. Saves me a lot of trouble of taking care of cleaning up unused memory. – Anshul Koka Apr 29 '15 at 22:18
1

Model.destroy() takes care of destroying the model on the server. Does one still need to manually delete the Javascript model?

Yes, you still need to manually delete the model. Model.destroy() does not delete it.

Similariy, I realize that View.remove() will remove the view from the DOM. How should one safely get rid of the view object?

Remove all references to it. If nothing is pointing at your object it will get garbage collected. Generally speaking, unless you go out of your way to make sure references to an object stay alive (such as global variables) or get careless with .on() you should be ok.

Note that using .listenTo() on your views instead of .on() makes your life easier. Memory leaks with views used to be a big problem when we just had .on()(See here for more).

Collection.reset() clears the models in the collection. Will this also delete the underlying Javascript model objects or do they need to be explicitly deleted?

How would one get rid of the collection object itself?

Does Javascript have a garbage collector and will it take care of deleting all such objects? As long as there's no remaining reference to your objects they will get garbage collected.

I already described the birds-eye view of this (make sure you're no longer referencing your objects!), but here is a pretty good article about garbage collection in the context of Backbone.

View.remove() only removes the view from the DOM. My guess is that one still needs to take care of removing/destroying the underlying Model and unbinding all event listeners. Is that correct?

Note that it also calls .stopListening(), which unbinds your listeners if you used .listenTo() in your views. You do not need to remove/destroy the underlying model to let it get GC'd, again as long as there are no remaining references to it. Using .listenTo() with a model is a reference to it, but if you .remove() your view that will remove that reference.

I often use _.bindAll to change the context of a function call. I have not come across a way to unbind this binding. My understanding says that this is unnecessary. Am I right?

It's not clear to me what you would be trying to achieve by "unbinding," so I would say that it isn't necessary.

Community
  • 1
  • 1
Lochlan
  • 2,666
  • 3
  • 23
  • 25