When using a JS hook, how do we send data to/from Ecto?
I am trying to integrate a library named Ploty.js into a LiveView app. Plotly.js is a graphics library and it requires a parent DOM node to attach itself to. I created a JS Hook in Phoenix and it works.
My hook:
import Plotly from "../vendor/plotly"
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken},
hooks:{
myPlot:{
mounted(){
let element = document.createElement("DIV");
element.id = "test";
console.log(element)
this.el.appendChild(element)
Plotly.newPlot("test", [{
type: "treemap",
labels: ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents: ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ]
}])
}
}
}
})
Pltly.js renders to a single DOM node in a LiveView:
<div id="myPlot" phx-hook="myPlot">
<!-- Plotly chart will be drawn inside this DIV -->
</div>
I want to move data to and from Ecto so that I can render the result in the graphics library. I am unsure what Phoenix tools are available for this, or where to focus my attention. It looks like I could do with with a fetch and hit an existing route that behaves as a data API but I am still curious about this.
Thank you