blockly_compressed.js: this.init is not a function

347 views
Skip to first unread message

Guilherme Recchi Cardozo

unread,
May 17, 2017, 1:00:27 PM5/17/17
to Blockly
Hi,

I'm using this definition generator so I can program in Typescript, but I still use the original javascript blockly library (using script tags in my index.html). 
I was able to inject blockly properly using:

this.workspace = Blockly.inject(DOMHandler.blocklyDiv,{toolbox: tBox});
this.workspace.addChangeListener(this.myUpdateFunction);

But when I try to get the code from the workspace:

public myUpdateFunction = (event:any) =>
{
let code:string = "no code yet";
code = this.blJSGenerator.workspaceToCode(this.workspace);
...


I receive the TypeError: "this.init is not a function" from blockly_compressed.js


Any ideas?

Thanks.


Rachel Fenichel

unread,
May 17, 2017, 2:20:22 PM5/17/17
to Blockly
Hi Guilherme,

Do you have more of a stack trace?

Are you able to generate javascript?  If so, maybe the generator is missing its init function.

If that doesn't work I suggest switching to uncompressed Blockly, which should give you a better stack trace.

Cheers,
Rachel

Guilherme Recchi Cardozo

unread,
May 17, 2017, 3:28:15 PM5/17/17
to Blockly
Here's the stack trace:


I'll check the generated Javascript, but I'm using the original Javascript Blockly library, I only use the d.ts definitions to get autocompletion in VS Code.
One thing that was different is that in Javascript I could just write:

var code = Blockly.Javascript.workspaceToCode(this.workspace);

But in Typescript in needed to instantiate a Blockly Javascript Generator:
this.blJSGenerator = new Blockly.Generator("JavaScript");

Is this a problem?

Guilherme Recchi Cardozo

unread,
May 17, 2017, 3:29:25 PM5/17/17
to Blockly
And one more thing, I'm quite new to both Typescript and Javascript.

Rachel Fenichel

unread,
May 17, 2017, 4:07:02 PM5/17/17
to Blockly
Hm, I'm not sure what's going on here, but it looks like it might be in how the wrapper is setting up Blockly.

I'm not familiar with typescript or that particular project--you may want to create an issue on that github repo.

Best,
Rachel

Erik Pasternak

unread,
May 17, 2017, 4:35:24 PM5/17/17
to Blockly
Hi Guilherme,

Because you're creating it manually you're missing a bunch of the setup that happens in generators/javascript.js. Blockly.Javascript gets configured by that file (which is included in javascript_compressed.js). Are you including both blockly_compressed.js and javascript_compressed.js?

Cheers,
Erik

Guilherme Recchi Cardozo

unread,
May 18, 2017, 3:47:00 PM5/18/17
to Blockly
Hi, I'm including all of this into my index.html:

<script type="text/javascript" src="libs/blockly/blockly_compressed.js"></script>
<script type="text/javascript" src="libs/blockly/blocks_compressed.js"></script>
<script type="text/javascript" src="libs/blockly/pt-br.js"></script>
<script type="text/javascript" src="libs/blockly/javascript_compressed.js"></script>

Rachel Fenichel

unread,
May 18, 2017, 4:57:20 PM5/18/17
to Blockly
If your generator inherits from Blockly.Generator, when you called workspaceToCode it should hit this line of code:
  this.init(workspace);
Which looks like it's the case here.  Your generator class should have defined an init function--it's not defined on the base generator.

For instance, here's the base generator definition of init, versus the Javascript generator definition.  

In the generator section of the code you linked to I don't see any definitions of init.

Guilherme Recchi Cardozo

unread,
May 19, 2017, 9:31:38 AM5/19/17
to Blockly
I was indeed having a lot of confusion with the code generator. I've posted this issue on Blockly-d.ts repository and the author explained my mistakes.

Thanks, Rachel!
Reply all
Reply to author
Forward
0 new messages