1

The following code from this guide employs a union type to hold all possible widget types, then render a view:

type Widget
    = ScatterPlot (List (Int, Int))
    | LogData (List String)
    | TimePlot (List (Time, Int))

view : Widget -> Element
view widget =
    case widget of
      ScatterPlot points ->
          viewScatterPlot points

      LogData logs ->
          flow down (map viewLog logs)

      TimePlot occurrences ->
          viewTimePlot occurrences 

My rather elementary question is:

With respect to the Widget type, what is the underlying model here?

Given an action (e.g. user wants to see a scatter graph widget), what is the underlying structure that should be updated?

Laurel
  • 5,965
  • 14
  • 31
  • 57
category
  • 2,113
  • 2
  • 22
  • 46

1 Answers1

2

Widget is your raw data. You then need to model separately which data to show

type alias Model = Widget

Then suppose that you have some incoming IO data

update action model = 
  case action of 
    ScatterPoints pts ->     -- List (Int, Int)
      ScatterPlot pts
    LogPoints pts ->
      LogData pts
    ...

then having loaded the data in the right type of Widget your view function from the original question will know what to do with the data

category
  • 2,113
  • 2
  • 22
  • 46
Simon H
  • 20,332
  • 14
  • 71
  • 128
  • Are permutations of 2 or 3 widget types outside the scope of the example? I was wondering what would need to be updated if the user wanted to switch from one widget type to another. – category Apr 26 '16 at 19:58
  • You would need a user event to trigger the update cycle, and a function that transforms the data to the new format – Simon H Apr 27 '16 at 05:22