Ethan Hohensee
Ethan Hohensee

Reputation: 1557

How to Include model data in Spark Java response for React JS to access?

I'm building a web app with Spark Java, OrientDB, and hopefully React.

With Spark Java and Velocity templates, I can access the server side data in the template and do my rendering logic based off of that. Since React is client side, how do I include that same data-model with the response so that I don't have to go back to the server with AJAX?

If I convert my POJO/Map/List to JSON, how can I include that data with the initial HTML/React without going back to the server to fetch it?

Upvotes: 1

Views: 610

Answers (1)

k.liakos
k.liakos

Reputation: 669

You should output an html script tag containing a javascript variable with your JSON serialized data.

Something like this:

<script>
window.myData = $myData;
</script>

Then you can access these data client side.

Upvotes: 1

Related Questions