Access attributes in blockly.

371 views
Skip to first unread message

Frederik Creemers

unread,
Jan 15, 2018, 3:17:46 PM1/15/18
to Blockly
I'm building a project for connecting several services together using blockly. These services can return fairly complex objects. Is there an existing way to allow the user to drill down in an object to get a value somewhere further down? The structure of the object is known in advance.

So for example, if I have a User object like this:

{
 
"id": "x0a1bc34df0zy",
 
"username": "John Doe",  
  "email": "jd...@example.com",
 
"address": {
   
"street": "Bogus Avenue",
   
"number": 404,
    "state": "BZ",
   
"country": "JK"
 
}
}



I'd like users to be able to select "address", then "street", but also just "email", and get the value. Is there a solution for something like this out there, or would I have to build a custom block for this? If so, how would you recommend going about this?

Andrew Stratton

unread,
Jan 15, 2018, 4:22:43 PM1/15/18
to Blockly
As a starting point - how about a function generated menu for the first set of keys - so this would return the top level names of the properties in the object.

The tricky bit is that when you choose address, you really want another menu to be created that is loaded from the next level down - but I would solve this after getting the first bit working.

Hard to see whether this would work without more detail of what you are trying to do...

Hope this helps
  Andy

Andrew Stratton

unread,
Jan 15, 2018, 4:26:22 PM1/15/18
to Blockly
Just thought - the second (and subsequent levels) could be autogenerated menus at the top level
i.e. the menu would show
  • id
  • username
  • email
  • address::street
  • address::number
  • ...

You could use anything you like instead of '::'.

Fairly simple to do - and the menu selection is then quite easy - just select one menu item.

Cheers
  Andy

Frederik Creemers

unread,
Jan 15, 2018, 4:56:01 PM1/15/18
to blo...@googlegroups.com
That's an interesting idea, but since these objects could get quite large, I'm thinking of making these blocks chainable. So in the first block you'd get the top level items, and based on your selection, you can attach another block to get the next level attribute, and so on.

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/gzhoRaAtViQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Stratton

unread,
Jan 16, 2018, 7:48:10 AM1/16/18
to Blockly
You could look at mutators (I haven't used these) so that when a drop down is chosen - and when there are further levels - you could show a socket for plugging in the next level down selection - and then use a sub-menu block that you drag in.

The concern I would have here is what happens when you change the original menu selection...

if you know the maximum depth of objects, you could just populate a block with that many menus and disable (or blank out) the not relevant ones - populating them on the fly as and when needed.  This could look a bit amateur - but should be quite quick to do.

Hope this helps
  Andy

Andrew Stratton

unread,
Jan 16, 2018, 7:48:53 AM1/16/18
to Blockly
...and - if you made all the irrelevant menus not visible, they could appear only when you need them...

Rachel Fenichel

unread,
Jan 16, 2018, 1:43:32 PM1/16/18
to Blockly
Some of the list blocks change shape or add/remove inputs when you change dropdowns.  For instance, these are all the same block:

But this approach makes it hard for the user to see what they can do from a glance at the toolbox.  I'd instead do multiple blocks that are chainable. 

Rachel Fenichel

unread,
Jan 16, 2018, 1:46:03 PM1/16/18
to Blockly

Reply all
Reply to author
Forward
0 new messages