Provide some initialisation data for javascript from the controller

I would like to provide a javascript calendar with its data when the page loads. My current idea was to hand the data from the controller to the template and to use it there within a tag.

In the controller:

render(conn, "index.html", my_data: some_data_as_json_string)

In the template:

<script> window.calendar.addEvents(<%= @my_data %>) </script>

Currently, this does not work unfortunately. Even though, I create the calendar in app.js and assign it to window.calendar, it is undefined within the tag - any ideas why? If I type it in the web console, it is defined.

But generally, I guess there must be a better approach for the whole problem than the one I chose. Is there?

Could be your JavaScript hasnˋt finished loading yet.