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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up