field.getSourceBlock throws exception

15 views
Skip to first unread message

Koen Van Wijk

unread,
Oct 13, 2022, 4:43:12 AM10/13/22
to Blockly
Hi,

With the 9 release field.getSourceBlock throws exception instead of returning a null if the source block is not connected yet.

My code look somewhat like:

Blockly.Blocks['generate_list_length'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Length of")
        .appendField(new Blockly.FieldDropdown(function() {
                  ....
                  var source_block = this.getSourceBlock()
                   if (source_block) {
                   }
So I call the getSourceBlock in the dynamic options functions. However this dynamic option function seems to be already called before the SourceBlock is connected. Now I have to make a ugly code like:

try {
  var source_block = this.getSourceBlock()
} catch (e) {
  var source_block = null;
}

Is this on purpose, did I miss something?

Regards,
Koen  

Aaron Dodson

unread,
Oct 14, 2022, 6:10:03 PM10/14/22
to Blockly
Hi,

This was intentional, but the usecase you're describing makes sense, so we're planning to revert this change in an upcoming patch release. Thanks for reporting this!

- Aaron

Reply all
Reply to author
Forward
0 new messages