fbonetti / elm-rails

View helpers for incorporating Elm modules into Rails views
MIT License
75 stars 20 forks source link

Example with a dynamic port #14

Open lsimoneau opened 7 years ago

lsimoneau commented 7 years ago

@fbonetti maybe you can clarify this for me. The current example in the README has a port with a value that's passed in as a "flag". If we want interop with a JavaScript library, we need to have access to the JavaScript variable referencing the Elm application. In my app I've solved this by ignoring the view helpers and just embedding my Elm app using the JS API directly. Is there a way to do this with the view helpers?

If not, I'd suggest we could deprecate the view helpers and update the README with an example using straight JS to manage the embed.

fbonetti commented 7 years ago

The primary reason I created this library was for the view helper (I was inspired by the react-rails gem), but is it limiting in the sense that it's hard to use ports with the current implementation. You can actually access the Elm app instance on the window object, albeit this is probably not the approach.

I can think of two solutions to this problem:

  1. Remove the view helper and just use Javascript
  2. Keep the view helper and attach the Elm app instance to the node itself. For example:
onLoad(function() {
-  window.#{raw module_name} = #{raw module_name}.embed(embedDiv, #{raw args.to_json});
+  embedDiv.elmApp = #{raw module_name}.embed(embedDiv, #{raw args.to_json});
});

Edit: I'm not sure which approach is better but I'm leaning towards deprecating the view helper