Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does backbone.js do with models that are not used anymore

Lately I am diving into the whole client-side MVC/MVVM design paterns, and the one I am particularly interested in is backbone.js.

One thing I do not fully understand is what happend to the models when they are not really needed anymore.

Let's say we have an app that has users and products. We have user models/views and product models/views

NOTE: for simplicity sake we are not a user. We can just CRUD users / products.

When I enter the products page, I assume we load the model and the view corresponding to this.

What happens when we leave the page and enter the users page. A user model/view is loaded, but the products are also still loaded.

Do we keep them loaded, does backbone take care of that for you, or do you explicitly need to end certain objects.

like image 778
Saif Bechan Avatar asked Mar 18 '12 12:03

Saif Bechan


People also ask

Does anyone still use BackboneJS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is the purpose of BackboneJS?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What is the only method available in the BackboneJS history?

There is only method named "start" can be used to manipulate the Backbone. js history.

What is BackboneJS comparable to?

Vue. js, React, AngularJS, Angular 2, and Ember. js are the most popular alternatives and competitors to Backbone. js.


2 Answers

Backbone does not explicitly handle cleaning up objects for you. It's 50/50 you and the JavaScript runtime.

JavaScript is a garbage collected language like Java, C#, Ruby, and others. The basic of a garbage collected languages is that an object that is still referenced by your application will not be cleaned up. The counter to that is when an object is no longer referenced by your application, will be cleaned up.

JavaScript in general:

When you create a variable, you can either scope that variable to a local function or as a global variable.

Global variables are never cleaned up by the garbage collector, during the life of the page. The only time they are cleaned up is when you leave the HTML page behind entirely - navigate to a different page and force the browser to load the new page from the server (doing a complete server refresh) or closing the browser or browser tab.

Function scoped variables are cleaned up when the variable falls out of scope - that is, when the function has exited and there are no more references to it. There are a few exceptions to this: return values and closures.

A return value is held in your app by assigning the return value to another variable. A return value falls under the same general rules, but the variable is now in a different function. Once that variable goes out of scope, it can be cleaned up.

A closure allows a parent scope to provide values that a descendant scope can access. When the descendant scope is cleaned up, the parent's closured variable may be allowed to be cleaned up (assuming nothing else is holding on to it).

Objects with attributes and functions fall under the same rules. An object can reference another object or function by having an attribute assigned to it: myObj.foo = thatObj.

The DOM (Document Object Model - the HTML in your app) is a JavaScript object. Events and other references to your DOM work the same as any other reference. If you have an object handling a DOM event, it has a reference in your app and it won't be cleaned up by the garbage collector. If you want it cleaned up, you have to remove all references to it - including the DOM reference from the event handler.

Cleaning Up Memory

The general rule is that if you are loading data in to a backbone collection or object and you want that object to be cleaned up so it's not using anymore memory, you must remove all references to that object. This is just the standard JavaScript garbage collection rule.

You cannot force garbage collection, but you can force a variable to de-reference the thing it points to using the delete keyword in JavaScript: delete myVar

Backbone

Backbone is JavaScript so it falls under the same rules. There are some interesting uses of closures and references in Backbone that you need to be aware of, which will help you to know when you need to manually clean up some objects.

For example: events. An even handler / callback method works by having a reference between the object that triggers the event and the callback that handles the event. This is one of the easiest places to cause memory leaks in a Backbone app and I discuss it in detail, here: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

Other than being aware of how events work in terms of references, just follow the standard rules for manage memory in JavaScript and you'll be fine. Once you remove all references to that collection of User objects, they'll be cleaned up.

like image 177
Derick Bailey Avatar answered Oct 19 '22 21:10

Derick Bailey


Note that if you console.log() your model (at least in Chrome) it will remain in memory because the console has a reference to it.

I got mad because of this. If you see memory leak, try clearing your console and run your profile again. If you had the same problem i had, it's gone :)

like image 21
lipsumar Avatar answered Oct 19 '22 20:10

lipsumar