Lessons learned about Elm from Slack
I share here my personal notes that I took from Slack Elm https://elmlang.slack.com
How can I wait until something specified in the view is in the DOM?
The most reliable way is to use window.requestAnimationFrame() and poll the DOM for the element you want
I need to chain one task to another. The second task should run if and only if the result of the first task satisfies a certain condition. How would I do that?
Task.andThen
is what you want. `andThen` will receive the result of the first task and you can use that to decide whether to return a task that does something else or just a Task.suceed with the original result.
When I am using Task.andThen, how can I see response from the first task?
`firstTask |> Task.andThen (\first -> secondTask first |> Task.map (\second -> (first, second)))`
How to get the value from a select html field?
Use on “change” (Json.Encode.map MyMessage targetValue)
, onInput seems not working in Opera
How can I Json encode a list of records {name: String, description: String}
so they can become part of the the body of a Http POST?
person =
object
[ (“name”, string “Tom”)
, (“age”, int 42)
]compact = encode 0 person
— {“name”:”Tom”,”age”:42}
or
Json.Encode.object
[ (“name”, Json.Encode.string nameString)
, (“key”, Json.Encode.<type> value)
, …
]
Is there a way I’m supposed to handle validation errors in the elm architecture?
I like the way @rtfeldman does it in http://package.elm-lang.org/packages/rtfeldman/elm-validate/latest
I’ve been loosely aware of warnings to not go overboard with nested Elm architectures. however — I’m curious if people have any good guidelines for me as to when it’s appropriate to do so. i’ve been following @rtfeldman’s excellent SPA example and “Scaling Elm” talk, and in those app(s) i see examples of nested architectures — so it must be appropriate to introduce at *some* level. any thoughts or opinions?
One heuristic you can follow is to have one init/update/view setup per-”page” in a multi page SPA like richard’s example. Then you sew all the pages up at the top with Cmd.map
and Html.map
and so on. More generally, it works when you have two or more isolated parts of an application that don’t need to modify any shared state.
What’s the role of uncurry function?
It allows to convert a function that takes in two arguments into a function that takes in a tuple. In other words, it allows you to pass a tuple to a function that takes multiple arguments without first having to destructure the tuple.
What’s the correct way to check a value as a valid integer? I’d like to validate a field input as an integer and otherwise, show the error message
You can use https://github.com/rtfeldman/elm-validate or
Result.withDefault 0 (String.toInt “42”) == 42
Result.withDefault 0 (String.toInt “ab”) == 0
What’s the easiest way of changing the 4th element of a List? [0,0,0,0,0] -> [0,0,0,42,0]
You can use http://package.elm-lang.org/packages/elm-lang/core/5.1.1/List#indexedMap. But if you’re doing that often you might want to use an Array instead http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Array#set