Cette page couvre Elm 0.18

Messages et données attachées

Il est possible d'attacher des données à vos messages :

module Main exposing (..)

import Html exposing (Html, button, div, text, program)
import Html.Events exposing (onClick)


-- MODEL


type alias Model =
    Int


init : ( Model, Cmd Msg )
init =
    ( 0, Cmd.none )



-- MESSAGES


type Msg
    = Increment Int



-- VIEW


view : Model -> Html Msg
view model =
    div []
        [ button [ onClick (Increment 2) ] [ text "+" ]
        , text (toString model)
        ]



-- UPDATE


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Increment howMuch ->
            ( model + howMuch, Cmd.none )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- MAIN


main : Program Never Model Msg
main =
    program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

Notez la manière dont le message Increment requiert un entier :

type Msg
    = Increment Int

Ensuite dans la vue, nous déclenchons ce message en lui attachant des données (ici un entier) :

onClick (Increment 2)

Et finalement, dans la fonction update, nous utilisons du pattern matching pour extraire les données :

update msg model =
    case msg of
        Increment howMuch ->
            ( model + howMuch, Cmd.none )

results matching ""

    No results matching ""