1

我刚刚发现了“块状”,这正是我想要将我的 webApp 提升到一个新水平的东西。我遇到的问题是我真的不明白如何启动 python 或 js 代码变量。

这是我的块:

Blockly.Language.myapp_ifdo = {
  helpUrl: '',
  init: function() {
    this.setColour(210);
    this.appendDummyInput()
        .appendTitle("if")
        .appendTitle(new Blockly.FieldDropdown([["Temperature", "Temperature"], ["Humidity", "Humidity"]]), "SENSOR")
        .appendTitle(" ")
        .appendTitle(new Blockly.FieldDropdown([["=", "="], ["≠", "≠"], ["<", "<"], ["≤", "≤"], [">", ">"], ["≥", "≥"]]), "OPERATOR")
        .appendTitle(" ")
        .appendTitle(new Blockly.FieldTextInput("0"), "SENSORVALUE");
    this.appendStatementInput("DO")
        .appendTitle("do");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip('');
  }
};

渲染是:

在此处输入图像描述

下拉列表内容:

在此处输入图像描述

我正在尝试做的事情:

如果选择了“温度”,那么我想在生成代码的开头初始化变量:

temperature = None
if temperature <= '30':
  pass

如果选择“湿度”也一样:

humidity = None
if humidity >= '60':
  pass

在我的“template.soy”文件中,我有这个:

<block type="myapp_ifdo"></block>

希望我足够清楚...感谢您的帮助!

问候,

4

1 回答 1

2

The built-in python generator will take care of this for you, if you use built-in Blockly variables and other constructs.

The code demo uses the function Blockly.Generator.workspaceToCode to generate code from the blocks. Once all the blocks have been processed, it calls into the generator's finish function to prepend variable declarations.

You can see the finish for yourself in python.js:

/**
 * Prepend the generated code with the variable definitions.
 * @param {string} code Generated code.
 * @return {string} Completed code.
 */
Blockly.Python.finish = function(code) { 
...

You will need to roll your own generator code if you can't use the built-in constructs from Blockly. You may be able to use this code from Blockly as a starting point, although it will be tricky because you will need to maintain your own list of variable declarations.

于 2013-09-10T19:47:23.337 回答