dwyl / learn-elm-architecture-in-javascript

:unicorn: Learn how to build web apps using the Elm Architecture in "vanilla" JavaScript (step-by-step TDD tutorial)!
https://todomvc-app.herokuapp.com
GNU General Public License v2.0
213 stars 19 forks source link

How would you handle HTTP requests? #67

Open 8483 opened 5 years ago

8483 commented 5 years ago

The ELM Architecture handles HTTP requests outside of the mount function, as it considers them impure.

Do we simply execute the request inside of the update switch statement i.e. break the pattern?

nelsonic commented 5 years ago

@8483 could you please share a bit more detail about your HTTP request? Are you sending/requesting data from a server/service? Elm typically does this with a Task. in JS land we would need to create an event listener.

8483 commented 5 years ago

@nelsonic That was a quick response. :)

Yes, I am talking about getting data from a RESTful API and updating the model with the response data.

Task and Command were always confusing for me compared to Message. I still don't fully understand why they are required and what the difference is.

As far as I got it, commands are used as a message sent after a message, and tasks as handling impure things.

It would be cool if they were explained via a Javascript example, but sadly, I am unable to contribute the explanation as I don't fully grasp them.

nelsonic commented 5 years ago

@8483 your understanding of Task, Command and Message are good. ✅ I tend to agree that in some cases explaining Elm Architecture concepts in JS is useful for people who have good JS knowledge/experience. But I think Evan is pitching his guide/language at people who aren't JS experienced ... 🤔

w-h-a commented 3 years ago

For what it is worth, I would definitely be interested in seeing the elmish Todo app expanded to elmishly talk to APIs/servers with vanilla JS. What would elm commands look like in the vanilla JS elmish Todo app? Would we also have to add on to elmish.js or perhaps make our own Http.js and JSON.js (for encoding form inputs) modules? I don't know if there's still any interest in this, but I would love to see something like that here. Following the tutorial so far has been very useful for me. If anyone knows of a similar elmish tutorial that also covers API/server communication, that would also interest me.