Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:elm-programming [2020/07/05 06:34] alfred [Maps] |
wiki2:elm-programming [2021/05/02 10:06] (actual) |
||
|---|---|---|---|
| Línea 64: | Línea 64: | ||
| in | in | ||
| ( model, cmd ) | ( model, cmd ) | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== Trigger after one calling ==== | ||
| + | |||
| + | You can do something like this for sending message ''UpdateTotals'' with value 100: | ||
| + | <code> | ||
| + | GetAccount result -> case result of | ||
| + | Ok account -> ({model | account = account }, Task.succeed (UpdateTotals 100.0) |> Task.perform identity) | ||
| + | Err err -> log (toString err) (model, Cmd.none) | ||
| + | </code> | ||
| + | |||
| + | Or just define: | ||
| + | <code> | ||
| + | send : msg -> Cmd msg | ||
| + | send msg = Task.succeed msg |> Task.perform identity | ||
| </code> | </code> | ||
| Línea 135: | Línea 151: | ||
| ==== Flags. Js values on app initialization ==== | ==== Flags. Js values on app initialization ==== | ||
| - | Flags are a way to pass values into Elm on initialization. | + | Flags are a way to pass values into Elm on initialization. The idea is that you use the type of those values that are passed to your script for produce the first model status. |
| + | |||
| + | So an initial function like this: | ||
| + | <code> | ||
| + | init : () -> (Model, Cmd Msg) | ||
| + | init _ = | ||
| + | ( initialModel, Http.get{ url = "/api/bookmarks", expect = Http.expectJson BookmarksReceived resultsDecoder} ) | ||
| + | </code> | ||
| + | |||
| + | With Flags will be similar to this: | ||
| + | <code> | ||
| + | init : FlagsType -> (Model, Cmd Msg) | ||
| + | </code> | ||
| In JS we can create the application with the chosen flags: | In JS we can create the application with the chosen flags: | ||
| Línea 314: | Línea 342: | ||
| ===== Encoders and Decoders ===== | ===== Encoders and Decoders ===== | ||
| - | ==== Succeed ==== | + | ==== Useful ==== |
| + | |||
| + | === succeed === | ||
| You can set a fixed value with ''Decode.succeed'': | You can set a fixed value with ''Decode.succeed'': | ||
| Línea 334: | Línea 365: | ||
| {- (D.maybe (D.field "groups" <| D.list groupDecoder)) -} | {- (D.maybe (D.field "groups" <| D.list groupDecoder)) -} | ||
| (D.maybe (D.succeed [])) | (D.maybe (D.succeed [])) | ||
| + | </code> | ||
| + | |||
| + | === oneOf === | ||
| + | |||
| + | It tries a decoder, if fails, tries the next one and so on: | ||
| + | <code> | ||
| + | groupDecoder : D.Decoder T.PageGroup | ||
| + | groupDecoder = D.map5 T.PageGroup | ||
| + | (D.field "uuid" D.string) | ||
| + | (D.field "title" D.string) | ||
| + | (D.oneOf [D.field "subtitle" D.string, D.succeed ""]) -- If subtitle is null, a different type than string... it will put "" | ||
| + | (D.field "order" D.int) | ||
| + | (D.field "pages" <| D.list pageDecoder) | ||
| </code> | </code> | ||
| Línea 411: | Línea 455: | ||
| ==== Create a program without a view ==== | ==== Create a program without a view ==== | ||
| Use ''Platform.worker''. Documentation [[https://package.elm-lang.org/packages/elm/core/1.0.5/Platform#worker|here]]. | Use ''Platform.worker''. Documentation [[https://package.elm-lang.org/packages/elm/core/1.0.5/Platform#worker|here]]. | ||
| - | ===== Gotchas ===== | ||
| - | Every function that accepts two arguments can be converted in one that accepts one argument: | ||
| - | <code> | ||
| - | > String.repeat | ||
| - | <function> : Int -> String -> String | ||
| - | |||
| - | > String.repeat 4 | ||
| - | <function> : String -> String | ||
| - | </code> | ||