Reputation: 11721
I am implementing a single page application.
I have a container div (<div id="container"/>
), where i load html content using AJAX
.
// function that is overwritten by loadMenu functions
// and gets called before loading a new section
function unbindPreviousSection() { };
// load contacts
function loadContactsMenu() {
unbindPreviousSection();
unbindPreviousSection = function () { };
$.get("/Home/Contacts", function (data, status) {
if (status === "success") {
$("#content").html(data);
contactsMenu.bind();
unbindPreviousSection = contactsMenu.unbind;
}
});
};
// load profile
function loadProfileMenu() {
unbindPreviousSection();
unbindPreviousSection = function () { };
$.get("/Home/Profile", function (data, status) {
if (status === "success") {
$("#content").html(data);
unbindPreviousSection = function() {
// specific unbind methods for this menu
};
}
});
};
var contactsMenu = {};
(function () {
var viewModel = null;
contactsMenu.bind = function () {
viewModel = {
phones: ko.observableArray()
};
};
contactsMenu.addPhone = function (phone) {
viewModel.phones.push(phone);
};
contactsMenu.unbind = function () {
viewModel = null;
};
}());
Inside any menu load function, i internally call unbind
method of the previous loaded menu.
loadContactsMenu();
loadProfileMenu(); // internally calls contactsMenu.unbind();
Before i load any data, i call unbindPreviousSection()
function to dispose the previous menu data.
My question is:
Is viewModel
variable inside contactsMenu
object still persisting after i call contactsMenu.unbind()
even if i set it to null? (does it creates memory leaks?)
Does contactsMenu.addPhone
function creates a closure which saves in memory viewModel
variable (because is used inside the function)?
Upvotes: 0
Views: 138
Reputation: 2208
I'd recommend to use Chrome profiler to check this and any further memory leak problems. You can take memory snapshot there and see for every object who is holding it in memory.
Upvotes: 1