Why there is no back end example or guid?


#1

hi there:

In Elm web site, I can hardly find any back end integration guide or example.
Is it so obvious about how to integrate with PHP, Python, JSP, Ruby, Asp.net,
etc. ? Everyone knows how to? or where may I get some more instructions?

Thanks!


#2

Elm is designed to be back end agnostic. You communicate with a back end via XHR requests that (probably) send and receive JSON. You can also send values into your program via flags or ports.

At work, we use Elm heavily inside of an MVC PHP application. We use ports to pass in a few initialization values from PHP that are then used throughout the Elm program during its runtime (while the user is on the page). In our code, for several of the Elm applications, you see initialization code like:

<script>
var node = document.getElementById('my-app');
var app = Elm.MyApp.embed(node, {
    userId: '<?php echo $current_user_data["user_id"]; ?>',
    userGroup: '<?php echo $current_user_data["user_group"]; ?>'
});
</script>

In the above example, the userId and userGroup values are passed to the Elm program when it is initialized. However, the actual values are determined by the PHP code as the template that this snippet is embedded in is rendered on the server. With this approach, the line that looks like:

userGroup: '<?php echo $current_user_data["user_group"]; ?>'

Will actually be received by the browser as something like:

userGroup: 'ADMINISTRATOR'

The ADMINISTRATOR value passed to the Elm program will then be decoded and may be used for something like cosmetic changes related to the user group (e.g. a ‘You are logged in as an Administrator’ message).

Disclaimer

When using this approach, do NOT trust that these values will not be tampered with. A malicious client may tamper with these values to change the behavior of your Elm program. Everything that the Elm program (or any client-side program) does should be independently validated by the server. For example, if the user tells my server they want data that only an administrator can see, the server independently validates that the user is actually an administrator based off of their server-side session data.

Hope that helps give some insight as to how you would pass values from some PHP code into an Elm application.


#3

hi, christian:

Thank you for your kindly explanation.
I saw this approach in ELM web site. However, I am not very confident that,
this approach make ELM look like a component (ex: ckeditor), rather than
a powerful SPA (ex: Angular).
I wander… where can I see some, full web site sample in ELM? including
routing, more libraries or modules (not just single file)? backend integration
will be nice. Thanks again!


#4

Check out rtfeldman/elm-spa-example! It’s a RealWorld example app that does all of that.


#5

Here’s a toy app that makes post requests and has a responsive UI.


#6

https://www.elm-tutorial.org/en/ has a very good step by step tutorial on building full stack multi page app, starting here:

https://www.elm-tutorial.org/en/04-starting/cover.html


#7

I think, this is a good example of the real SPA app with Elm and Node.js server.


#8

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.