I want to create some divs dynamically. Each div contains an object
function node(id, title, content, isPrivate, dateOfCreation) { // my node object
this.id = id;
this.title = title;
this.content = content;
this.isPrivate = isPrivate;
this.dateOfCreation = dateOfCreation;
this.lastEdited = dateOfCreation;
}
and this object is managed by my datastore class
var store = new dataStore(); // instance of the store
function dataStore() {
this.nodes = []; // all the node objects
this.getNodes = function() { // get all objects
return this.nodes;
}
this.addNode = function(node) { // add a new object
addElementToArray(this.nodes, node);
}
this.deleteNode = function(node) { // delete an object
deleteElementFromArray(this.nodes, node)
}
this.getNodeById = function(nodeId){ // find an object by its id
for(var i = 0; i < this.nodes.length; i++){
if(nodeId == this.nodes[i].id)
return this.nodes[i];
}
return null;
}
this.getNodeTitle = function(node){ // get the title of the node
return node.title;
}
}
So I want to create some divs, one div for each object.
function initNodeView() { // build up all the divs
for (var i = 0; i < 30; i++) { // -- TEST -- Fill the store
store.addNode(new node(i, i, i, i % 2 == 0, i));
}
var nodes = store.getNodes(); // get all the nodes from the store
for (var i = 0; i < nodes.length; i++) {
var currentNode = nodes[i]; // the current object
var nodeContainer = createDiv('#nodeList', "nodeContainer"); // the wrapperdiv
createDivWithText(nodeContainer, "nodeContentDiv", store.getNodeTitle(currentNode)); // create a child div with the title of the object
var barContainer = createDiv(nodeContainer, "nodeBtnBarDiv"); // create a childdiv - the container of all buttons
createDivWithIcon(barContainer, "nodeIcon", currentNode.isPrivate ? "'foo.png'" : "'bar.png'"); // create an icon div
var btnDefaultCss = "nodeBtn"; // default css class for buttons
createBtn(barContainer, btnDefaultCss, "Edit", function() {
// empty ..
});
createBtn(barContainer, btnDefaultCss, "Delete", function() {
nodeContainer.remove(); // remove this container from the DOM
store.deleteNode(currentNode); // delete this object from the store
});
}
}
To refactor my code I built up some helper functions.
function createDiv(parent, cssClass){ // create default div
var divElement = $("<div></div>");
divElement.addClass(cssClass);
$(parent).append(divElement);
return divElement;
}
function createDivWithText(parent, cssClass, text){ // create div with text
var divElement = createDiv(parent, cssClass);
divElement.html(text);
return divElement;
}
function createDivWithIcon(parent, cssClass, iconSource){ // create icon div
var divElement = createDiv(parent, cssClass);
var icon = $("<img src='"+ iconSource +"'/>");
divElement.append(icon);
return divElement;
}
function createBtn(parent, cssClass, text, fn){ // create a new button
var btn = $("<button></button>");
btn.addClass(cssClass);
btn.html(text);
btn.click(function() {
fn();
});
$(parent).append(btn);
}
My problem is, when calling initNodeView() the div containers are built up fine. But when clicking on a button (edit or delete) and want to log the stored object of this container it always got the last object stored in it.
So when creating 30 objects and 30 div containers they all got the 30th object stored in it.
Normally div 1 should have object 1, div 2 object 2, ....