Add attributes to xml while initialization of Blockly.Blocks

409 views
Skip to first unread message

Vaclav Rajce

unread,
Feb 16, 2016, 8:18:56 PM2/16/16
to Blockly
Hello,
I had two questions:

1) Add attributes to xml while initialization of Blockly.Blocks
I just decided to write serveside XML parser to JAVA while I had one written in javascript using your libs, but after a while, I get to problem, that inside blockly generated XML like this:

 <block type="controls_if" id="5" inline="false" x="40" y="11">
    <value name="IF0">
      <block type="logic_compare" id="21" inline="true">
        <field name="OP">EQ</field>
        <value name="A">
          <block type="someObject" id="26"></block>
        </value>
        <value name="B">
          <block type="someObject" id="4">
            <field name="someObject">"1"</field>
          </block>
        </value>
      </block>
    </value>
  </block>

where block one_half usually should contain some other attributes as I modified prototype of the element that I can set addition info

Blockly.Block.prototype.getAdditionalInfo = function(something) {
return this.additionalInfo;
}
Blockly.Block.prototype.setAdditionalInfo = function(something) {
this.additionalInfo=something;
}

while in initialization of block I add the parameter
this.setAdditionalInfo('identifier123');

and generator with generating code as below:
var code = 'someObject("'+this.getAdditinalInfo()+'")';

So javascript generator knows it has to generate code as below:
if( someObject('identifier123') == 1 ){

}

But XML doesnt contain the information, so Im not on the serverside obtain additional information. The engine behind blockly is too much complicated to make some other change inside core without any pointing, but I assume this feature is already implemented and its just about pointing me out.


2) Hardcode identifier of block to be equal with XML identifier? 
Also I had question that I see that xml uses IDs inside the blocks so I assume that in case xml will be stored, then javascript will change in case the Blockly.Blocks['identifier'] which was defined second (had generated id 2 by blockly) and now is before first (had generated id 1 by blockly) will show different blockly scheme then was generated while stored right? Is there something to avoid this? For example setting the ID of block while initialization or something?
Message has been deleted

Vaclav Rajce

unread,
Feb 16, 2016, 8:36:43 PM2/16/16
to Blockly
1) So I solved also the problem #1 where inside blockly/core/xml.js
inside function blockToDom i added function below:
if(block.additionalInfo!=null){
element.setAttribute('additionalInfo', block.additionalInfo);
}

and at blockly/core/block.js inside function Blockly.Block.prototype.initialize
this.id = Blockly.genUid();
this.additionalInfo=null;


Dne středa 17. února 2016 2:18:56 UTC+1 Vaclav Rajce napsal(a):
Reply all
Reply to author
Forward
Message has been deleted
0 new messages