ValueTree <-> GUI data binding (suggestion for ValueTree)


In order to have faster results i want to bind GUI-elements directly to values in the value tree, which is very easy to do in juce.

ValueTree data("data")

toggleButton.getToggleStateValue().referTo(data.getPropertyAsValue("toggleValue", nullptr));`

This works perfect.

But what if i want to replace the data with a new data (maybe if the user chooses a preset)

data = presetData

The problem is, that toggleButton still referring to the old “data” ValueTree. To make this work again i have to “re-bind” the data, which is not very elegant.

And here comes my suggestion, it would be extraordinary cool if ValueTree has a method, which just copies the information from one ValueTree to another, complete recursive, without out touching the references.

So instead

data = presetData

we do


in this case, all bindings will persist.

Is there such a function or wouldn’t it be nice to have this functionality?


It has a copyPropertiesFrom() method, but when I looked into doing a recursive function like you’re suggesting for the entire trees, I hit all kinds of nasty edge-cases involving listeners, so decided to deliberately not provide such a method.


Thanks for your opinion, but what would you think is best practice, to handle this, without loosing the comfort of data binding.

Sure I could do it all manual, implement a listener to follow the changes, or make a special accessor class for the valueTree, but than its complicated again.


Well if you’ve got listeners then you just have to have a way of re-creating/re-binding your listeners when the tree changes. That’s not a big deal to do, because you’ll need to have written something that does the initial creation/binding of GUI elements to a tree when you load a new one from storage. So just call that same setup routine if you swap out the whole tree for a new one.