Event Listener Blocks added/connected and so on

1,164 views
Skip to first unread message

medieninf

unread,
Sep 2, 2014, 4:55:53 AM9/2/14
to blo...@googlegroups.com
Hi,

I'm new to blockly. I have read the wiki pages. But I miss a tutorial/API how I can listen to change events on the workspace. E.g. if a block is added/connected.

As far as I understand I can add a change listener with:

Blockly.addChangeListener(workspaceChanged());

 var workspaceChanged = function() {
            console.log("changed");
}

But how can I determine the kind of change event? Or what if I only want to listen to a certain kind of event.


Thanks and best regards.

Alex

Eric Miller

unread,
Sep 2, 2014, 10:28:14 AM9/2/14
to blo...@googlegroups.com
First, the proper line is Blockly.addChangeListener(workspaceChanged); //No parens after workspaceChanged
Secondly, as far as I know the best way to determine the type of change event is to inspect the workspace and track the states you care about. For example, if you monitor the total number of blocks, you can distinguish between block addition, deletion, and moves. Usually, workspace change events are used to do things like constantly regenerating code, or displaying error messages, or something. Most of those don't require any sort of state information, they can simply completely regenerate their target by looking at the workspace's current state.

Chris Jackson

unread,
Sep 2, 2014, 12:02:44 PM9/2/14
to blo...@googlegroups.com
Following on from Alex’s original post, I do agree that it would be useful if the library provided some information on the reason for the change rather than requiring the user to do this. The workspaceChanged even gets fired a lot more often than “real” changes (i.e. changes to the design). If you move an unconnected block for example, it will fire, so technically the workspace has changed, but most users probably don’t care since the “program” hasn’t changed. It seems a bit inefficient to have to rescan the blocks to find out if something “really” changed.

Something for an enhancement maybe :)

Cheers
Chris

medieninf

unread,
Sep 4, 2014, 8:47:11 AM9/4/14
to blo...@googlegroups.com
Hi @all,

thanks for the reply. What if with changes concerning variables? How can I listen if a variable is renamed?

Thanks alex
Reply all
Reply to author
Forward
0 new messages