Backbone.ModelBinder
Backbone.ModelBinder copied to clipboard
ModelBinder requires non-standard and non-documented close() function in views
I regularly come across the following warning in JS logs:
warning, you should implement a close() function for your view, you might end up with zombies
ModelBinder emits this from removeEl() function.
The official Backbone documentation does not mention any function named close(), specifically not for views. Also ModelBinder's Readme does not mention close() anywhere. However, Backbone 0.9.6 introduced View#remove, which correctly unbinds events in addition to removing element from DOM. I think you should expect and call remove() instead of close().
Agreed.
I've already adopted this tact by adding a function to my standard wrapper on View:
close: function() { this.remove(); }
Agree :+1:
At some point in the near future, I will just add a check to see if the remove function is defined on the view and call that. I need to keep close around for a large code base.
Remove is always defined as it's part of the API. I suppose it could call both for backwards compatibility?
katowulf is right. Every class derived from Backbone.View has the remove() function.
Since ModelBinder 1.0.0 the required version of Backbone is 1.0.0, so I think there is no point having any checks for "backward compatibility" for features introduced already in Backbone 0.9.6.
@Derberth right, my point in backwards compatibility was with ModelBinder, not Backbone--some people could be relying on the existence of the close function. So maybe it should continue to check for that, or maybe not, that's a tough call.
As an aside, it would be nice to be able to specify what that function should be. At my company, we have a pattern where we implement a dispose()
function which does pretty much exactly the same thing, and it's somewhat annoying that this one relies on close()
. Maybe if it were an optional thing (in a new release, since that's backwards-compatible), then the view factory could simply use whatever function name is passed in as an option. Much more flexible!