DEV Community


Keeping TreeView state with component in Go

Jeff Lindsay
Indie maker of many things. Helped pioneer SHDH, webhooks, Hacker Dojo, TIGSource, OpenStack, JWT, Twilio, Docker, Dokku, all incidental. Follow your curiosity
・1 min read

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.

Discussion (0)