Created
August 23, 2016 08:23
-
-
Save samidarko/8b263f06c4874d2f0758d152b6ef0869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (Html) | |
import Html.App | |
import Widget | |
-- MODEL | |
type alias AppModel = | |
{ widgetModels : List Widget.Model | |
} | |
initialModel : AppModel | |
initialModel = | |
{ | |
widgetModels = [Widget.initialModel, Widget.initialModel] | |
} | |
init : ( AppModel, Cmd Msg ) | |
init = | |
( initialModel, Cmd.none ) | |
-- MESSAGES | |
type Msg | |
= WidgetMsg Int Widget.Msg | |
-- VIEW | |
view : AppModel -> Html Msg | |
view model = | |
Html.div [] | |
(List.map2 (\m i -> Html.App.map (WidgetMsg i) (Widget.view m)) model.widgetModels [0..(List.length model.widgetModels)-1]) | |
-- UPDATE | |
getItem: Int -> List a -> Maybe a | |
getItem a b = List.head (List.drop a b) | |
update : Msg -> AppModel -> ( AppModel, Cmd Msg ) | |
update message model = -- (model, Cmd.none) | |
case message of | |
WidgetMsg pos subMsg -> case (getItem pos model.widgetModels) of | |
Just m -> let ( updatedWidgetModel, widgetCmd ) = | |
Widget.update subMsg m | |
in | |
( { model | widgetModels = List.map2 (\m i -> if i == pos then updatedWidgetModel else m) model.widgetModels [0..(List.length model.widgetModels)-1] }, Cmd.map (WidgetMsg pos) widgetCmd ) | |
Nothing -> (model, Cmd.none) | |
-- SUBSCIPTIONS | |
subscriptions : AppModel -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- APP | |
main : Program Never | |
main = | |
Html.App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment