Last active
March 29, 2016 08:02
-
-
Save daphee/b2af8e7aafc83b78497b 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 ColorPicker where | |
import Model exposing (initialModel) | |
import Action exposing (update) | |
import View exposing (view) | |
type Action = | |
InitialsSaved Bool | |
| Noop | |
app = | |
StartApp.start { | |
init = (initialModel, Effects.none), | |
view = view, | |
update = update, | |
inputs = [incomingGameObject] | |
} |
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 Model (Model) | |
import Button.Model as Button | |
type alias Model = | |
-- I am using a Maybe here to be able to indicate a removed button | |
{ buttonModel : Maybe Button.Model | |
} | |
initialModel : Model | |
initialModel = | |
{ buttonModel = Just Button.initialModel | |
} |
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
-- I am not sure if Action(..) is correct. | |
-- I know that to import possible values for a type this works but I tend to expose everything in my app | |
module Action (Action(..), type) where | |
-- Top/App Level update | |
-- I like to define my action type at the same place I have my update function | |
type Action = | |
RemoveButton | |
-- I explain this in more detail in the button's view function | |
ButtonAction Button.Action | |
| Noop | |
-- Update: Something happened --> Update the model to contain the changes | |
update :: Action -> Model -> (Model, Effects Action) | |
update action model = | |
case action of | |
Noop -> | |
-- Nothing happened. This action isn't necessary | |
-- I need it mostly if I have a Task which's result I want to ignore | |
(model, Effects.none) | |
ButtonAction action | |
-- Some action for the button module happened | |
-- Let the button update handle it | |
let | |
-- we ignore effects here | |
(buttonModel, _) = Button.update action model.buttonModel | |
in | |
({model | buttonModel = buttonModel}, Effects.none) | |
RemoveButton -> | |
({model | buttonModel = Nothing}, Effects.none) |
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 View (view) where | |
import Action exposing (Action) | |
import Model exposing (Model) | |
import Button.View as Button | |
-- Top/App Level view | |
-- View: give me a model and *one* address where I can send actions/events to | |
view :: Signal.Address Action -> Model -> Model | |
view address model = | |
-- Some wrapper for the button | |
div | |
[] | |
[ | |
-- ForwardTo does nothing but label actions | |
-- Everytime an action arrives (from the button module) the "label" ButtonAction is added to let the | |
-- root update function forward the action to the button module's update function | |
-- It's like an additional "box" around the original action | |
-- If you have multiple modules nested each view puts a box around the actions of its children which leads to multiple | |
-- nested boxes which get unpacked on after another in the update functions | |
Button.view | |
-- I discuss this context in more detail in the button/view.elm file | |
-- The always works because the second parameter to forwardTo is actually a function | |
-- In the second forwardTo the funciton is the ButtonAction type. It has a type variable which gets filled this way | |
-- The type variable is the content of the box | |
-- In the case of the button removal we don't want that the button module needs to know about the RemoveButton | |
-- action and send it itself. We know that every value that arrives at this address is meant to indicate | |
-- that the button is removed. That's why always is a perfect fit. It just discards the value sent and always returns | |
-- RemoveButton | |
{ removeAddress = Signal.forwardTo address (always Action.RemoveButton) } | |
(Signal.forwardTo address Action.ButtonAction) | |
model.button |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment