blocklygoogle-blockly

how can i get the code of the blocks connected to my custom block by blockly


everyone.

Hope you are doing well.

I have created my custom block using block factory.

The code for block definition is

Blockly.Blocks['web'] = {
init: function() {
this.appendDummyInput()
    .appendField("When ")
    .appendField(new Blockly.FieldDropdown([["button1","OPTIONNAME"]]), "NAME")
    .appendField(".Click");
this.appendStatementInput("NAME")
    .setCheck(null)
    .appendField("do");
this.setColour(120);
 this.setTooltip("Triggers when the button is clicked");
this.setHelpUrl("");
  }
 };

And the code for Generator stub:

Blockly.JavaScript['web'] = function(block) {
    var dropdown_name = block.getFieldValue('NAME');
     var statements_name = Blockly.JavaScript.statementToCode(block, 'NAME');
    // TODO: Assemble JavaScript into code variable.
    var code = '$(button).on("click", function(){})';
    return code;
};

And the block is

Block

How can i get the code of the blocks which are added to this button1 block.

please guide me i am new to blockly.

Regards,shiva


Solution

  • Doesn't your statements_name variable contain the data you need?

    code = `var code = \$(${statements_name}).on("click", function(){});`
    

    Note how I'm escaping your "$" to not interfere with the template string format.

    (Sidenote: You also have to get the statements in the "do" statement input. You are using the same name for it: "NAME" which is going to be problematic)