Skip to content

Instantly share code, notes, and snippets.

@pdamoc
Created February 24, 2016 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdamoc/bdfbd49f98d3259d8f27 to your computer and use it in GitHub Desktop.
Save pdamoc/bdfbd49f98d3259d8f27 to your computer and use it in GitHub Desktop.
InterComponentCommunication update
module Application where
import Html exposing (..)
import Html.Events exposing (on, onClick)
import Components
import SelectedComponents
type alias Model =
{
components : List String,
selectedComponents : List String
}
type Action = ComponentsAction Components.Action
init : Model
init =
{
components = ["component1", "component2", "component3"],
selectedComponents = []
}
update : Action -> Model -> Model
update action model =
case action of
ComponentsAction componentAction ->
let
(components', selected) = Components.update componentAction model.components
selectedComponents' =
case selected of
Nothing -> model.selectedComponents
Just component -> SelectedComponents.addComponent component model.selectedComponents
in
{ model | components = components', selectedComponents = selectedComponents' }
view : Signal.Address Action -> Model -> Html
view address model =
div [] [Components.view (Signal.forwardTo address ComponentsAction) model.components, SelectedComponents.view model.selectedComponents]
module Component where
import Html exposing (..)
type alias Model = String
view : Model -> Html
view model =
div [] [ text model ]
module Components where
import Html exposing (..)
import Html.Events exposing (on, onClick)
import Component
import Time
type alias Model = List String
type Action = Select String
update : Action -> Model -> (Model, Maybe String)
update action model =
case action of
Select name ->
let
(model', selected) = List.partition (\c -> c /= name ) model
in
(model', List.head selected)
view : Signal.Address Action -> Model -> Html
view address model =
section [] ((h1 [] [text "Components"]) :: (List.map (\c -> div [] [(Component.view c), (button [onClick address (Select c)] [text "Select"])]) model))
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "3.0.0 <= v < 4.0.0",
"evancz/elm-html": "4.0.2 <= v < 5.0.0",
"evancz/start-app": "2.0.2 <= v < 3.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
import Application
import StartApp.Simple exposing (start)
main =
start
{
model = Application.init,
update = Application.update,
view = Application.view
}
module SelectedComponents where
import Html exposing (..)
import Component
import Time
type alias Model = List String
type Action = AddComponent String
update : Action -> Model -> Model
update action model =
case action of
AddComponent name -> name :: model
addComponent component model =
update (AddComponent component) model
view : Model -> Html
view model =
section [] ((h1 [] [text "Selected Components"]) :: (List.map Component.view model))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment