Adding a button to a toolbox without categories

422 views
Skip to first unread message

spa...@gmail.com

unread,
Nov 11, 2017, 9:33:57 AM11/11/17
to Blockly
Hello,

If I have categories, I'm able to add buttons and callback functions by using callbackKey. If I try to do the same with a simple toolbox that has no categories, I can't get the callback to work.

This one works:
  <xml id="toolbox" style="display: none">
    <category name="test">
      <button text="test" callbackKey="TEST"></button>
    </category>
  </xml>
...
      workspace.registerButtonCallback('TEST', function(button) {
        alert("foo");
      });

This does not:
  <xml id="toolbox" style="display: none">
    <button text="test" callbackKey="TEST"></button>
  </xml>
...
      workspace.registerButtonCallback('TEST', function(button) {
        alert("foo");
      });

The button gets created, but it the callback does not work. Any idea?

Victor Ng

unread,
Nov 14, 2017, 4:54:19 PM11/14/17
to Blockly
You've definitely found a bug with Blockly. The problem is a chicken and egg one:
- Blockly.inject(...) will create the toolbox button and in doing so, maps the button's callback to one that's registered on the workspace. If the callback isn't registered, the button doesn't get associated with the callback.
- However, you can't actually register a callback on the workspace until after Blockly.inject(...) completes.

The reason this isn't a problem with categories is because the toolbox button isn't created until later, when the category is actually shown. 

-----

The simple workaround for now is to call the following after registerButtonCallback(...):

workspace.updateToolbox(document.getElementById('toolbox'));

We will file a bug to fix this though, so you don't have to do this in the future.

spa...@gmail.com

unread,
Nov 16, 2017, 11:35:47 AM11/16/17
to Blockly
Ah. Makes perfect sense. Thanks for the workaround.
Reply all
Reply to author
Forward
0 new messages