I maybe post it differently.

I have this small program that asynchronously reading data from JSON file 
and displays it as an array.

import Html exposing (..)
import Html.App as App
import Http
import Table exposing (defaultCustomizations)
import Task
import Json.Decode as Json exposing ((:=))



main =
 App.program
 { init = init
 , update = update
 , view = view
 , subscriptions = \_ -> Sub.none
 }



-- MODEL

type alias Job =
 { task : String
 , who : String
 , place : String
 }

type alias Jobs = List Job

type alias Model =
 { jobs : Maybe Jobs
 , tableState : Table.State
 }


init : ( Model, Cmd Msg )
init =
 ( { jobs = Nothing
 , tableState = Table.initialSort "Year"
 }
 , getJson
 )



-- UPDATE


type Msg
 = SetTableState Table.State
 | FetchSucceed (Maybe Jobs)
 | FetchFail Http.Error


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
 case msg of
 SetTableState newState ->
 ( { model | tableState = newState }
 , Cmd.none
 )

 FetchSucceed jobs ->
 ( {model | jobs = jobs}
 , Cmd.none
 )

 FetchFail _ ->
 (model, Cmd.none)



-- VIEW


view : Model -> Html Msg
view { jobs, tableState } =
 div [] [ viewJobs jobs tableState ]

viewJobs maybeJobs tableState =
 case maybeJobs of
 Nothing ->
 text "No jobs to display"
 Just jobs ->
 Table.view config tableState jobs


-- TABLE CONFIGURATION


config : Table.Config Job Msg
config =
 Table.customConfig
 { toId = .task
 , toMsg = SetTableState
 , columns =
 [ Table.stringColumn "Task" .task
 , Table.stringColumn "Who" .who
 , Table.stringColumn "Place" .place
 ]
 , customizations =
 defaultCustomizations
 }



-- HTTP


getJson : Cmd Msg
getJson =
 Task.perform FetchFail FetchSucceed (Http.get decoderColl "jobs.json" |> 
Task.toMaybe)


decoder : Json.Decoder Job
decoder =
 Json.object3 Job
 ("task" := Json.string)
 ("who" := Json.string)
 ("place" := Json.string)


decoderColl : Json.Decoder Jobs
decoderColl =
 Json.object1 identity
 ("jobs" := Json.list decoder)


Now I have the second usecase when same data has to be printed out as a 
list. My question is is there a way to re-use JSON reading routine?

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to