I haven't done this yet as we're developing the UI first as things take
shape and faking the API calls with Process.sleep + Task.perform. So below
is just a sketch.
Conceptually, you could use `Http.getString` (or construct a request and
use `Http.send`) in Elm to get the JSON, then send that through a port to
JavaScript. You'd need at least one outgoing port, and an incoming port for
every result type.
port deserializeJson : String -> String -> Cmd msg
port studentsLoaded : (List Student -> msg) -> Sub msg
port coursesLoaded : (List Course -> msg) -> Sub msg
subscriptions : Model -> Sub Msg subscriptions model = Sub.batch [
studentLoaded StudentsLoaded , courseLoaded CoursesLoaded ]
On the outgoing port, you could pass both the JSON and the incoming port
name as a string. Then in the JS, you could wire it once like this:
app.ports.deserializeJson.subscribe(function(port, json) {
// TODO can throw, so catch error and send to an error port
var object = JSON.parse(json);
app.ports[port].send(object);
});
Once you get the HTTP response string from a particular request, you send
it through the outgoing port with the correct return path.
deserializeJson "studentLoaded" json
It looks like after initial setup, each deserialization would take ~3 lines
of wiring code. So it might not be worth it for small objects. But again
this is just a sketch that I haven't tried to run.
On Tuesday, October 4, 2016 at 6:40:08 AM UTC-5, Vojtěch Král wrote:
>
> I have the same problem. Would someone be so kind as to give an example of
> how to launder the JSON via ports like this? Do you just rut the data
> through a port, or do you do ajax in JS?
> Thanks!
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.