Extensions and Mixins JavaScript

219 views
Skip to first unread message

Bryan langhendries

unread,
Apr 21, 2023, 8:27:37 AM4/21/23
to Blockly
Hello,

I have been trying to implement an extension for my block.
My goal: Every block of a certain type that gets created would then initialize the value of a field 'id' with a unique id.
Example: Create 3 blocks of type 'a_type', the first block added to the workspace then has a value 1 for this.id_, the second block 2 and so on.

I have read the documentation and I have gone through the source code but I haven't found a way to add an extension when using the JavaScript definition for blocks. 

Is there a way to achieve this?

Kind regards,
Bryan

Beka Westberg

unread,
Apr 21, 2023, 12:24:33 PM4/21/23
to blo...@googlegroups.com
Hello Bryan =) Thanks for your question!

When you're defining things with JavaScript, any functions you put into your block definition will get mixed into the eventual block instance.

For example:
```
Blockly.Blocks['my_block'] = {
  init: function() {
    // all your fancy init logic
  },

  myFirstMixin: function () { }, // This gets mixed in
  mySecondMixin: function() { }, // This also gets mixed in
}
```

So for your block you might do something like:
```
Blockly.Blocks['my_block'] = {
  init: function() {
    this.appendDummyInput()
        .appendField(new Blockly.FieldLabel(this.generateId()));
  },

  generateId: function() {
    return 1; // Actually do something to gen your ID
  }
}
```

I hope that helps! If you have any further questions please reply =)
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/e1928467-0164-41a9-ae62-e59c127fd2b5n%40googlegroups.com.

Bryan langhendries

unread,
Apr 21, 2023, 5:17:54 PM4/21/23
to Blockly
Hello,

I forgot to add that I am only using it as an extra state variable and do not want to display it on the block. 
similar to this.id_ = ...
and since I am allowing the usage of multiple workspaces (Scratch-like interface for sprites) I need to load the blocks into a hidden workspace at the end to convert the workspaces to code.
Thus the init function cannot be used to initialize the parameter since the block will be created multiple times but the id only has to be created once on its first creation. And not whenever the workspace gets loaded in again.

Thank you for the fast reply!
Kind regards,
Bryan

Op vrijdag 21 april 2023 om 18:24:33 UTC+2 schreef bwes...@google.com:

Beka Westberg

unread,
Apr 21, 2023, 6:05:29 PM4/21/23
to blo...@googlegroups.com
Hello again!

It sounds like you want to make sure your data property is round-tripped through serialization? In that case you can either use the data property of the block, or you can create a mutator. These allow you to append extra data to the serialized state of the block =)

But if I'm misunderstanding your problem please tell me and I'm happy to try to suggest an alternative solution!

I hope that helps! If you have any further questions please reply,
--Beka

Bryan langhendries

unread,
Apr 21, 2023, 7:02:25 PM4/21/23
to Blockly
Would you have any tips/example as how I could implement this then? To summarize, I need the block to hold as extra state an id which it gets from looking at the current value of a dropdown menu. And this should only be initialized once as the block is first created. Thank you very much is advance!
Kindest regards,
Bryan
Op zaterdag 22 april 2023 om 00:05:29 UTC+2 schreef bwes...@google.com:

Beka Westberg

unread,
Apr 24, 2023, 11:11:33 AM4/24/23
to blo...@googlegroups.com
Hello,

Did you try implementing my previous suggestions (a data property or mutator)? The linked text goes to some docs that should help with figuring out what to do (but the docs can admittedly be confusing)!

If you did try implementing them, could you explain a little bit about what problems/blockers you ran into? If you didn't, could you be a bit more specific about what question you're trying to answer?

Hopefully with a bit more information we can find a workable solution =)
--Beka

Bryan langhendries

unread,
Apr 24, 2023, 11:26:49 AM4/24/23
to Blockly
Hello,
I apologize! I must have done something terribly wrong the first time when I tried to implement the data property. It seems this solved my issue! Thank you very much, this was a very big help!
Have a good week!
Bryan

Op maandag 24 april 2023 om 17:11:33 UTC+2 schreef bwes...@google.com:

Beka Westberg

unread,
Apr 24, 2023, 1:48:42 PM4/24/23
to blo...@googlegroups.com
Yay glad you found something that worked for you! Best of luck on your project :D

If you have any further questions always feel free to ask! And I hope you have a lovely week as well =)

Best wishes,
--Beka

Reply all
Reply to author
Forward
0 new messages