DEV Community

Jeff Lindsay
Jeff Lindsay

Posted on

2

Keeping TreeView state with component in Go

I came back to the tree view prototype re-implemented in my Go UI framework. It really doesn't behave any differently, but I spent all evening reimplementing it to handle a more React-like data model, where we store authoritative state of the tree in Go with the component, load and save there, and then push that state to the jQuery component.

I wasted a ton of time just trying to find a way to reset the data in jsTree. I kept trying to recreate the element and then re-run the initialization JavaScript that had the data to load. It really didn't like this, but I didn't think of another way until later when I put the jsTree element outside the component, then removed the template and just directly started setting up and using the jsTree API from the Go component.

Then I had to hook up any change that happens in jsTree to reproduce that change in the data model in the Go component, saving, and re-rendering the component, which now just calls some JS to set and refresh jsTree.

Lastly I got localStorage persistence working so it's roughly where it was before, but now I have control over the nodes in Go and can store any extra data on them there instead of hacking jsTree to keep extra data on the nodes.

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay