找到你要的答案

Q:How do I get a value in the `onchange` event of a Blockly custom block?

Q:我怎么一blockly自定义块` onchange `事件的价值?

I am making a custom block for Blockly and need to validate the inputs. In the onchange event, I want to warn the user if they are entering an invalid value for the input.

Here is my block:

Blockly.Blocks['motor'] = {
    init: function() {
        this.setHelpUrl('http://www.example.com/');
        this.setColour(65);
        this.appendDummyInput()
            .appendField("motor( ");
        this.appendValueInput("port_number")
            .setCheck("Number");
        this.appendDummyInput()
            .appendField(");");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
    },
    onchange: function(ev) {
        if (this.getFieldValue('port_number') > '3') {
            this.setWarningText('Port must be 0 - 3.');
        } else {
            this.setWarningText(null);
        }
    }
};

On the Blockly Developers Page, it has a basic example of getting an input value. However, I keep getting an undefined returned every time the onchange fires.

How can I handle validation for these inputs? I don't want to create a dropdown for the input because I need to be able to input from variables, int blocks, etc.

我将blockly自定义块和需要验证的输入。在onChange事件,我想向用户发出警告,如果他们进入输入无效值。

这里是我的街区:

Blockly.Blocks['motor'] = {
    init: function() {
        this.setHelpUrl('http://www.example.com/');
        this.setColour(65);
        this.appendDummyInput()
            .appendField("motor( ");
        this.appendValueInput("port_number")
            .setCheck("Number");
        this.appendDummyInput()
            .appendField(");");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
    },
    onchange: function(ev) {
        if (this.getFieldValue('port_number') > '3') {
            this.setWarningText('Port must be 0 - 3.');
        } else {
            this.setWarningText(null);
        }
    }
};

在blockly开发者页面,它有一个基本的输入值的例子。然而,我总是被一个未定义的每一次变动火灾返回。

如何处理这些输入的验证?我不想创建一个用于输入下拉因为我需要能够输入变量,int块,等。

answer1: 回答1:

Not sure if this is the best way to handle this, but it is working for me. I just access the input value using the valueToCode method. Then I can validate the input value.

Note: The context of the onchange handler is the block, so passing this as the first argument to Blockly.C.valueToCode will get the value from the correct block.

Blockly.Blocks['motor'] = {
    init: function() {
        this.setHelpUrl('http://www.example.com/');
        this.setColour(65);
        this.appendDummyInput()
            .appendField("motor( ");
        this.appendValueInput("port_number")
            .setCheck("Number");
        this.appendDummyInput()
            .appendField(");");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
    },
    onchange: function(ev) {
        var port_number = Blockly.C.valueToCode(this, 'port_number', Blockly.C.ORDER_ATOMIC);
        var valid = VALIDATE.motor_port_number(port_number);
        if (!valid) 
            alert("WARNING: The value for the motor port must be 0, 1, 2 or 3.");
        }
    }
};

不知道这是不是最好的方法来处理这个,但它是为我工作。我只是访问输入值使用valuetocode方法。然后我可以验证输入值。

Note: The context of the onchange handler is the block, so passing this as the first argument to Blockly.C.valueToCode will get the value from the correct block.

Blockly.Blocks['motor'] = {
    init: function() {
        this.setHelpUrl('http://www.example.com/');
        this.setColour(65);
        this.appendDummyInput()
            .appendField("motor( ");
        this.appendValueInput("port_number")
            .setCheck("Number");
        this.appendDummyInput()
            .appendField(");");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('');
    },
    onchange: function(ev) {
        var port_number = Blockly.C.valueToCode(this, 'port_number', Blockly.C.ORDER_ATOMIC);
        var valid = VALIDATE.motor_port_number(port_number);
        if (!valid) 
            alert("WARNING: The value for the motor port must be 0, 1, 2 or 3.");
        }
    }
};
javascript  blockly