GithubHelp home page GithubHelp logo

Working with Turbolinks about elm-rails HOT 3 OPEN

fbonetti avatar fbonetti commented on May 21, 2024
Working with Turbolinks

from elm-rails.

Comments (3)

kirchner avatar kirchner commented on May 21, 2024

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.

aaronshim avatar aaronshim commented on May 21, 2024

@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.

kirchner avatar kirchner commented on May 21, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.