Blockly Mutator 变量未更新

问题描述

我正在尝试制作一个 Mutator 以允许使用一组用户指定的属性创建对象。我想要一些类似于函数的 mutator 的东西,您可以在其中添加一个参数,它会更新参数名称

目前我有代码工作来验证名称自动生成新的属性名称,但是 mutator 块不会更新文本,这意味着它被我设置为的原始文本卡住了(在这种情况下为property0) .从此处拖动新块时,新块以无冲突的方式正确标记。谁能帮我让 mutator UI 更新值?

这看起来像的一个例子是 here一个新的和正确标记的块已被拖离 mutator 的冲突“property0”块。用户可以编辑此块,但如前所述,拖动新块时该值不会自动更新。

我的mutator块代码如下

Blockly.Blocks["object_new_prop"] = {
  init: function() {
    this.nameField_ = new Blockly.FieldTextInput("",this.validator_);
    this.appendDummyinput()
        .appendField("Property: ")
        .appendField(this.nameField_,"NAME");
    this.setPrevIoUsstatement(true);
    this.setNextStatement(true);
    this.setColour(Blockly.Constants.Object.HUE);
    this.setTooltip("Adds an additional property to object deFinition.");
    this.contextMenu = false;

    // correctly label the field label
    this.genNextLabel_();
  },/**
   * Generates and sets a valid non-conflicting label for a property name.
   * @return the next label used for a property name.
   **/
  genNextLabel_: function() {
    var defaultName = "property",i = 0;
    while (this.nameField_.validator_(defaultName + i) === null) {
      ++i;
    }
    this.nameField_.setValue(defaultName + i);
    console.log(this);
    return defaultName + i;
  },/**
   * Validates a property name choice.
   * @param propname the property name given to the field.
   * @return the property name if valid,otherwise null.
   **/
  validator_: function(propname) {
    // remove repeated whitespace,and leading and trailing whitespace from property name
    propname = propname.replace(/[\s\xa0]+/g,"").replace(/^ | $/g,'');
    if (!propname) {
      return null;
    }

    // prevent duplicate property names
    var sourceBlock = this.getSourceBlock();
    var workspace = sourceBlock.workspace.targetWorkspace ||
        sourceBlock.workspace;
    var caselessName = propname.toLowerCase();

    // iterate all blocks in mutator workspace
    var blocks = workspace.getAllBlocks(false);
    for (var i = 0; i < blocks.length; i++) {
      if (blocks[i].id == sourceBlock.id) {
        // skip container block
        continue;
      }

      // check for same value
      var otherVar = blocks[i].getFieldValue("NAME");
      if (otherVar && otherVar.toLowerCase() == caselessName) {
        return null;
      }
    }

    // valid,return value
    return propname;
  },}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...