Elm Language
구독
수색…
비고
구독은 입력을 듣는 수단입니다. 들어오는 포트 , 키보드 또는 마우스 이벤트, WebSocket 메시지, 위치 정보 및 페이지 가시성 변경 사항은 모두 입력으로 사용할 수 있습니다.
'탈퇴'가 포함 된 Time.every 이벤트의 기본 구독
0.18.0
모델이 구독으로 전달되면 모든 상태 변경으로 구독을 수정할 수 있습니다.
import Html exposing ( Html, div, text, button )
import Html.Events exposing ( onClick )
import Time
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ time: Time.Time
, suspended: Bool
}
init : (Model, Cmd Msg)
init =
( Model 0 False, Cmd.none )
-- UPDATE
type Msg
= Tick Time.Time
| SuspendToggle
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Tick newTime ->
( { model | time = newTime }, Cmd.none )
SuspendToggle ->
( { model | suspended = not model.suspended }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
if model.suspended then
Sub.none
else
Time.every Time.second Tick
-- VIEW
view : Model -> Html Msg
view model =
div []
[ div [] [ text <| toString model ]
, button [ onClick SuspendToggle ] [ text ( if model.suspended then "Resume" else "Suspend" ) ]
]
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow