I'm also generating a a toolbox dynamically form an array. But is does not seem to render it correctly. I have placed an alert in the init function which shows the correct item and while creating the blocks they also show the correct value. But when the loop is done all is nicely rendered but all the items added with the loop suddenly show the last item in the list. I have simplified my code, which reproduces this. The tooltip also shows "Number: 5" with every block:
Blockly.inject(document.getElementById('blocklyDiv'), {path: './js/libs/blockly/', toolbox: document.getElementById('toolbox')});
try {
for(var i=0;i<data.result.data.length;i++){
var xml = "";
var device = data.result.data[i];
//xml += '<category name="'+device.friendlyname+'" id="device_'+
device.id+'">';
var groupData = device.commandgroups;
for(var j=0;j<groupData.length;j++){
var commandGroup = groupData[j];
for(var k=0;k<commandGroup.commands.length;k++){
var curCommand = commandGroup.commands[k];
switch(curCommand.commandtype){
case "data":
init: function() {
this.setColour(120);
this.appendDummyInput()
.appendField(device.friendlyname);
this.appendDummyInput()
.appendField(curCommand.typedetails.label);
this.setOutput(true);
this.setTooltip('Number: ' + i);
alert(device.friendlyname);
}
};
break;
}
}
}
//xml += '</category>';
$('#toolbox').append(xml);
Blockly.updateToolbox(document.getElementById('toolbox'));
}
} catch(err){ alert(err); }
});
</script>