Dreamer
Dreamer

Reputation: 7549

Dojo: how to delete/destroy a node from NodeList for real

Here I have a list of node and an identified node that want to remove from the list(both from view and also in memory).

var currentRow = dojo.query(this).parents("div").first()[0];
var parentDiv = dojo.query(currentRow).parents("div").first()[0];
var rowList = dojo.query(parentDiv).children();

The problem is,

dojo.destroy(currentRow);

only remove the currentRow from view, but when I loop the rowList, from firebug I can still see the size of rowList is orginal size without minus the one remove, so it seem stay in memory, so how can we remove a node from nodeList and can return the new nodeList?

Upvotes: 0

Views: 2625

Answers (1)

phusick
phusick

Reputation: 7352

Destroying the node does not manipulate the rowList array, it just removes the node from the DOM tree. It means you have currentRow node cached in rowList array (and also in currentRow variable). DOM Nodes are JavaScript objects, when you remove them from the DOM tree they become later garbage collected by the JavaScript virtual machine, if they are not reachable.

In this case you just need to query rowList after (or again after) calling dojo.destroy:

var currentRow = dojo.query(this).parents("div").first()[0];
var parentDiv = dojo.query(currentRow).parents("div").first()[0];

dojo.destroy(currentRow);
currentRow = null;

var rowList = dojo.query(parentDiv).children();

If currentRow is globally reachable set it to null.

See it in action: http://jsfiddle.net/phusick/9MaUn/

Read more in Addy Osmani's Writing Fast, Memory-Efficient JavaScript.

Upvotes: 1

Related Questions