I'm new to using jQuery/javascript and am working on using the jsPlumb library to build some dynamic nodes, where the user can click on a shape and a new draggable node will appear.
My issue is I am building the list through a for loop, and the last element iterated over is being assigned to all other "clicks".
Here is the offending piece of code:
var sourceNodes = {
InputFiles:{
cssClass:".data_source",
name:"Input Files1",
options:[{
name:"Input Files1",
connectsFrom:[],
connectsTo:["IF1.1", "IF1.2"],
},
{
name:"Input Files2",
connectsFrom:[],
connectsTo:["IF1.1"],
}]
},
InputFiles2:{
cssClass:".data_source",
name:"Input2.1",
options:[{
name:"Input2.1",
connectsFrom:["Input1"],
connectsTo:["Input2.1"],
},
{
name:"Input2.2",
connectsFrom:["Input1"],
connectsTo:["Input2.2"],
}]
},
InputFiles3:{
cssClass:".data_source",
name:"Input3.1",
options:[{
name:"Input3.1",
connectsFrom:["Input2"],
connectsTo:["Input3.1"],
}]
}
};
for( var m in sourceNodes ){
var sourceNode = sourceNodes[m];
var sourceClass = jsPlumb.getSelector(sourceNode.cssClass);
console.log(sourceClass);
sourceClass.bind("click", function(connection){
var selectorClass = sourceNode.cssClass.concat("_window").substring(1);
var toFind = "div[id^='".concat(selectorClass,"']");
var DivIndex = $('#render').find(toFind).length+1;
var DivName = selectorClass.concat(DivIndex);
var Div = $('<div>', { id: DivName },
{ class: selectorClass });
Div.css({
top: connection.clientY,
left: connection.clientX,
});
Div.appendTo('#render');
Div.text(sourceNode.name);
jsPlumb.draggable($(Div));
$(Div).addClass(selectorClass);
_addEndpoints(DivName, ["BottomCenter"], ["TopCenter"]);
var selectOption = $('<select>', { id: DivName });
for( var n=0; n< sourceNode.options.length; n++){
var option = $('<option>', { value: n,
connectsTo: sourceNode.options[n].connectsTo,
connectsFrom: sourceNode.options[n].connectsFrom });
option.text(sourceNode.options[n].name);
$(selectOption).append(option);
}
$(Div).append(selectOption);
});
};
What happens is each item wihin sourceNodes that is clicked take on the values in InputFiles3, which is the last entry that is bound via sourceClass.bind. Any insight into what concept I am missing here, and how I can make each object within the loop independent?
delegateeither. It was superseeded byonas well.