I have some code like this:
var User = function () {
_.bindAll(this)
this.UserList = Backbone.Collection.extend({
model: UserModel
// ...
});
this.UserListView = Backbone.View.extend({
// ... etc
}
User.prototype.display = function() {
var self = this
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
var user = new User()
route("users", function () {
user.display()
})
My question is, is this going to cause memory issues? Every time the user arrives on the user/:userPage route the view, collection, etc are all going to be recreated. Will the old ones be deleted or do I have to delete it manually?
Should I be doing this:
User.prototype.display = function() {
var self = this
delete self.collection
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
delete self.users
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
Also other general advice on my example code is appreciated too.
This is utterly useless:
All
deletedoes is removecollectionfield from the self object, and then you immediately recreate it. Just do the assignment.I think that you think that
delete self.collectionshould somehow cause the garbage-collection of the object pointed to by the field. It doesn’t.Also, don’t use
selfas a variable name. The browser uses it to mean, uh, something.