Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Last active July 31, 2017 07:37
Show Gist options
  • Select an option

  • Save Janiczek/55761beea60bcfe1545bec02d5db7aff to your computer and use it in GitHub Desktop.

Select an option

Save Janiczek/55761beea60bcfe1545bec02d5db7aff to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Foo
import Bar
type alias Model =
{ myOwnData : Int
, foo : Foo.Model
, bar : Bar.Model
}
type Msg
= MyOwnMsg
| FooMsg Foo.Msg
| BarMsg Bar.Msg
init : Model
init =
{ myOwnData = 1
, foo = Foo.init
, bar = Bar.init
}
update : Msg -> Model -> Model
update msg model =
case msg of
MyOwnMsg ->
-- do something
FooMsg fooMsg ->
{model | foo = Foo.update fooMsg model.foo}
BarMsg barMsg ->
{model | bar = Bar.update barMsg model.bar}
view : Model -> Html Msg
view model =
div []
[ myOwnView
, Foo.view model.foo
|> Html.map FooMsg
, Bar.view model.bar
|> Html.map BarMsg
]
module Main exposing (..)
import Foo
import Bar
type alias Model =
{ myOwnData : Int
, foo : Foo.Model
, bar : Bar.Model
}
type Msg
= MyOwnMsg
| FooMsg Foo.Msg
| BarMsg Bar.Msg
init : (Model, Cmd Msg)
init =
let
(foo, fooCmd) = Foo.init
(bar, barCmd) = Bar.init
in
( { myOwnData = 1
, foo = foo
, bar = bar
}
, Cmd.batch
[ myOwnCmd
, fooCmd
|> Cmd.map FooMsg
, barCmd
|> Cmd.map BarMsg
]
)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
MyOwnMsg ->
-- do something
FooMsg fooMsg ->
let
(newFoo, fooCmd) =
Foo.update fooMsg model.foo
in
( {model | foo = newFoo}
, fooCmd
|> Cmd.map FooMsg
)
BarMsg barMsg ->
let
(newBar, barCmd) =
Bar.update barMsg model.bar
in
( {model | bar = newBar}
, barCmd
|> Cmd.map BarMsg
)
subscriptions : Model -> Sub Msg
subscriptions model =
let
fooSub =
Foo.subscriptions model.foo
|> Sub.map FooMsg
barSub =
Bar.subscriptions model.bar
|> Sub.map BarMsg
in
Sub.batch [myOwnSub, fooSub, barSub]
view : Model -> Html Msg
view model =
div []
[ myOwnView
, Foo.view model.foo
|> Html.map FooMsg
, Bar.view model.bar
|> Html.map BarMsg
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment