Injecting Variables into a workspace

681 views
Skip to first unread message

Vivek Kb

unread,
Feb 6, 2019, 6:22:28 AM2/6/19
to Blockly
Hi, I have just started using Blockly and have integrated with my html web page.
I have a set of variables in my database which I need to  bring into the workspace in order to make user create blocks using them.
Is there a way in which I can populate a set of variables from a database into my  current Blockly workspace?

kushal bhattacharya

unread,
Feb 6, 2019, 7:36:42 AM2/6/19
to Blockly
Could you please elaborate as to how you will be using the variables from database ?

Amber B

unread,
Feb 6, 2019, 8:50:22 AM2/6/19
to Blockly
Try something like this:

var createVariables = ['variable_one', 'variable_two', 'variable_three'];
var workspace_ = Blockly.inject('my-workspace-id', options);


Blockly.JavaScript.variableDB_.setVariableMap(workspace_.getVariableMap());


createVariables
.forEach(function(varName) {
    workspace_
.createVariable(varName);
});


Of note is that this will automatically generate its own variable IDs. For your purposes this is probably fine, but for other purposes, you may want to specify the variable ID parameter. Likewise, this doesn't include variable types, but you could specify those by changing the createVariables array to be an array of objects with the varName and varType (as well as varId if desired) and then just pass those into createVariable. Likewise, if you need access to those variables later, you can change the forEach loop to a map loop which returns the result of createVariable and then use that array later. You can see more documentation on createVariable here.

Depending on how you intend to set the variable values, you may need to get a bit creative when generating code to prevent the automatic variable definition at the top from occurring. If you're stuck on this step, let me know and I'll dig up an example of where we did just that in order to give you a pointer.

Vivek Kb

unread,
Feb 7, 2019, 12:26:45 AM2/7/19
to blo...@googlegroups.com
Hi,
I am not able to call the function setVariableMap as it says function variableDB does not exist.
This is the error I'm getting ->  Uncaught TypeError: Cannot read property 'setVariableMap' of undefined
This is my code -
var workspace = Blockly.inject('blocklyDiv',
{media: '../../media/',
toolbox: document.getElementById('toolbox')});
var createVariables = ['variable_one', 'variable_two', 'variable_three'];
Blockly.JavaScript.variableDB_.setVariableMap(workspace.getVariableMap());
createVariables.forEach(function(varName) {
console.log('name -' + varName);
workspace.createVariable(varName);
});

On Wed, Feb 6, 2019 at 7:46 PM Amber B <abla...@citizendeveloper.com> wrote:
Boxbe This message is eligible for Automatic Cleanup! (abla...@citizendeveloper.com) Add cleanup rule | More info
--
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.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Vivek Kb

unread,
Feb 7, 2019, 1:51:31 AM2/7/19
to Blockly
Yes the function 'Blockly.Javascript.variableDB_' is defined in the javascript.js file that is imported

On Wednesday, February 6, 2019 at 4:52:28 PM UTC+5:30, Vivek Kb wrote:
Message has been deleted

Vivek Kb

unread,
Feb 7, 2019, 2:15:01 AM2/7/19
to Blockly
It is not present in the Blockly.Javascript object when i tried on the console ,

pic-1.PNG

However it is present in the javascript_compressed.js file,

pic2.PNG



On Wednesday, February 6, 2019 at 4:52:28 PM UTC+5:30, Vivek Kb wrote:
Message has been deleted
Message has been deleted

Vivek Kb

unread,
Feb 7, 2019, 2:18:01 AM2/7/19
to Blockly
The compressed version of the file. Is there something wrong with that?


On Wednesday, February 6, 2019 at 4:52:28 PM UTC+5:30, Vivek Kb wrote:
Message has been deleted
Message has been deleted
Message has been deleted

kushal bhattacharya

unread,
Feb 7, 2019, 2:31:54 AM2/7/19
to Blockly
var createVariables = ['variable_one', 'variable_two', 'variable_three'];


 
 

 
createVariables.forEach(function(varName) {
                    console.log('name -' + varName);
                    workspace.createVariable(varName);
                  });
Use this code.This is creating variables in the variable category.No need to pass the rest of the code above posted earlier

 

Vivek Kb

unread,
Feb 7, 2019, 2:35:23 AM2/7/19
to Blockly
I included the uncompressed version of the file.
When i print the object it is not present in it.

pic3.PNG



On Wednesday, February 6, 2019 at 4:52:28 PM UTC+5:30, Vivek Kb wrote:

kushal bhattacharya

unread,
Feb 7, 2019, 2:37:07 AM2/7/19
to Blockly
use the updated code i posted above.

var createVariables = ['variable_one', 'variable_two', 'variable_three'];




createVariables.forEach(function(varName) {
                    console.log('name -' + varName);
                    workspace.createVariable(varName);
                  });

kushal bhattacharya

unread,
Feb 7, 2019, 2:37:48 AM2/7/19
to Blockly
When i use this code i get :-
Screenshot from 2019-02-07 13-03-00.png

kushal bhattacharya

unread,
Feb 7, 2019, 2:39:30 AM2/7/19
to Blockly
the behaviour you are getting is the obvious and correct one.Since you haven't yet called the init method of the generator object.
So 'Blockly.JavaScript.variableDB_' hasn't been instatiated yet.

Vivek Kb

unread,
Feb 7, 2019, 3:31:55 AM2/7/19
to Blockly
Thanks a lot Kushal:) This worked perfectly.


On Wednesday, February 6, 2019 at 4:52:28 PM UTC+5:30, Vivek Kb wrote:

Amber B

unread,
Feb 7, 2019, 8:43:25 AM2/7/19
to Blockly
Oops, sorry about that confusion -- I based that code off of a similar setup we have where that line is necessary and missed removing it. Glad you got it figured out! Thanks, Kushal!

kushal bhattacharya

unread,
Feb 7, 2019, 2:08:09 PM2/7/19
to Blockly
most welcome :)

Madhumati hosamani

unread,
Jan 27, 2020, 8:20:19 AM1/27/20
to Blockly
Hi Amber, Can we make same variable visible across workspace ?

Amber B

unread,
Jan 27, 2020, 9:23:27 AM1/27/20
to Blockly
Do you mean having them sync up such that adding/deleting a variable to/from one workspace affects the other instantaneously, or?

Madhumati hosamani

unread,
Jan 28, 2020, 1:08:15 AM1/28/20
to Blockly
yes Amber.. Can we achieve that ?

Amber B

unread,
Jan 28, 2020, 9:09:29 AM1/28/20
to Blockly
Oof, that would be a tough one... the way we set it up involved populating variables when a workspace loaded, not totally syncing them. I think you might be able to set up both workspaces with the same variable map in order to do this? Otherwise, you'd have to add event listeners on both workspaces and ensure that creating, deleting or renaming a variable on one did it on the other.

Madhumati hosamani

unread,
Jan 29, 2020, 12:42:26 AM1/29/20
to Blockly
Thank you Amber. 
at present I solved this by maintaining variable map globally available for all the workspace and creating and deleting them on load for each workspace.
while creating variable, it takes workspace as one of the parameter. can we not set multiple workspace for variable ?

Amber B

unread,
Jan 29, 2020, 9:07:43 AM1/29/20
to Blockly
I don't believe that we can set multiple workspaces for variables, unfortunately... you'd need to file a feature request or pull request with the changes on the Blockly project on git
Reply all
Reply to author
Forward
0 new messages