Implementation using requestAnimationFrame ()
Nowadays, this seems like the cleanest solution.
var container = document.getElementById('app'); var demoApp = Elm.RenderDemo.embed(container); var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; //Cross browser support var myPerfectlyTimedFunc = function(cellID) { requestAnimationFrame(function() { if(document.getElementById('cell:' + cellID) === null) { window.alert("Cannot find cell " + cellID) } }) } demoApp.ports.onCellAdded.subscribe(myPerfectlyTimedFunc);
See here for setting up a SPA type with multiple pages and the need to re-display the JS interop'd graph . It also has the ability to update the value of the data inside the chart. (Console log messages can also be helpful.)
If you are interested in how this can be implemented on the Elm side instead of the html / js side, see the Elm Defer Command library .
As you described, the problem is this:
- Javascript loads and searches for an element that has not yet been created.
- Elm displays the DOM after this search, and the item you need appears.
- Any Elm commands that you send through the port will also be executed in or before the render, so any javascript called by subscribing to the port will have the same problem.
Elm uses requestAnimationFrame (rAF) in itself as a way to handle the DOM queue, and not without reason. Let's say Elm does a few DOM manipulations in less than 1/60 of a second, instead of processing each manipulation individually - which would be pretty inefficient - Elm will pass them to the rAF browser, which will act as a buffer / queue for general DOM rendering, In other words, view is called in the animation frame after update , so the view call will not always be executed after each update .
In the past, people would use:
setInterval(someAnimationFunc, 16.6) //16.6ms for 60fps
requestAnimationFrame has emerged as a way for the browser to save the queue that it controls and runs through 60 frames per second. It offers a number of improvements:
- The browser can optimize the rendering, so the animation will be smoother
- Animation in inactive tabs will stop, allowing the processor to cool
- More battery
More information about rAF here , and here and the video here
My personal story began when I tried to display the chart of Chartist.js in a div, originally created in Elm. I also wanted to have multiple pages (SPA style), and the chart would have to be re-displayed when the div was recreated with various page changes.
I wrote the div as direct HTML in the index, but this prevented me from SPA functionality. I also used ports and subscriptions with jQuery ala $(window).load(tellElmToReRender) , and also provided Arrive.js a go - but each of them led to various errors and lack of desired functionality. I messed up a bit with rAF, but used it in the wrong place and wrong. This was after listening to ElmTown - Episode 4 - JS Interop , where I saw the light and realized how to really use it.