Comments (3)
I think, when using turbolinks, rather then adding an event listener for DOMContentLoaded
, you have to use the events provided by turbolinks (here is a list). So the js loading your elm app should look sth like this:
document.addEventListener('turbolinks:load', () => {
const node = document.getElementById('elm-container');
Elm.Main.embed(node);
});
You still could run into the problem that your elm app is loaded several times on revisits to the page containing this javascript. (Not on hard reloads using the browser.)
from elm-rails.
@kirchner Are there suggestions on how to avoid the problem of the Elm app loading several times? I've tried the following approach but I'm wondering whether there are saner solutions:
document.addEventListener('turbolinks:load', () => {
// Only load if Elm hasn't previously loaded
if (document.getElementsByClassName('elm-already-run').length == 0) {
// Use global state to mark as already having run-- eww!
let marker = document.createElement('div');
marker.className = 'elm-already-run';
document.body.appendChild(marker);
const node = document.getElementById('elm-container');
Elm.Main.embed(node);
}
})
from elm-rails.
@aaronshim we ended up doing sth like this
// app/javascript/packs/elm_app.js
import Elm from ‘ElmApp/Main’
$(“#elm-container”).empty();
const container = $(“#elm-container”)
const flags = JSON.parse($(“#elm-flags”).data(“data-flags”))
var app = Elm.ElmApp.Main.embed(container, flags)
where the html template looks like this
// app/views/foos/show.html.slim
#elm-container
#elm-flags(data-flags[email protected]_json)
= javascript_pack_tag “elm_app”
probably not so much better i think =)
from elm-rails.
Related Issues (14)
- Installation HOT 1
- Update the README with working example for ELM 0.17+ HOT 2
- Elm 0.18 HOT 2
- Rails generator?
- Example with a dynamic port HOT 1
- I cannot find module... HOT 4
- Debug Flag HOT 3
- Dependency Tracking HOT 1
- requiring modules in different directories under assets/elm? HOT 2
- README is talking about app/assets/javascript.js HOT 1
- I cannot release a new version HOT 3
- Documentation on where to run elm-package HOT 1
- Disabling caching slows down development considerably HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elm-rails.