Elm 0.17 - JSON数据无法在视图中呈现

时间:2016-05-16 22:28:03

标签: json elm

我是Elm的新手并试图找出0.17。我想知道是否有人了解我的数据为什么不在此处的视图中呈现。在Chrome控制台中,我可以看到GET请求已成功完成,但它实际上从未显示在页面上。我从URL返回的数据应该是这样的:

[{" title":" Aut enim quia accusamus。"," content":" Ipsa aliquid repellendus laudantium v​​elit。使用corrupti qui ut aut autudududndae。 Ipsum molestiae ullam quasi molestiae qui quas。错误nobis cupiditate dolor moortiae officia quod doloribus。"},{" title":" Architecto assumenda incidunt reprehenderit aut。"," content":& #34; Praesentium v​​oluptas aspernatur ullam voluptatum odit voluptas。 Tempora dolorum incidunt qui commodi fugit。 Adipisci maiores cum aliquid itaque voluptatem beatae。 Aut minus omnis nihil voluptatem。 Eius praesentium quis deleniti dolorem debitis。"}]

module Questions exposing (..)

import List
import Html exposing (..)
import Html.App
import Html.Attributes exposing (..)
import Html.Events as Events
import Http
import Json.Decode as Json exposing ((:=))
import Task exposing (..)

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

--MODEL

type alias Question =
  { title : String
  , content : String }

type alias Model =
  { questions : List Question
  }

initialModel : Model
initialModel =
  { questions = []
  }

questionsDecoder : Json.Decoder (List Question)
questionsDecoder =
  Json.list questionDecoder

questionDecoder : Json.Decoder Question
questionDecoder =
  Json.object2
        Question
          ("title" := Json.string)
          ("content" := Json.string)

fetchData : Cmd Msg
fetchData =
  let
    url = "http://localhost:3000/api/questions"
  in
    Task.perform FetchFail FetchDone (Http.get questionsDecoder url)

--INIT

init : ( Model, Cmd Msg )
init =
  ( initialModel
  , fetchData )

--UPDATE

type Msg = NoOp
    | FetchData
    | FetchDone (List Question)
    | FetchFail Http.Error

update : Msg -> Model -> (Model, Cmd Msg)
update message model =
  case message of
    NoOp ->
      ( model, Cmd.none )
    FetchData ->
      ( model, fetchData)
    FetchDone results ->
      ( { model | questions = results }
      , Cmd.none )
    FetchFail error ->
      ( model, Cmd.none )

-- VIEW

questionView : Model -> Question -> Html Msg
questionView model question =
  div [ ]
    [ h3 [  ] [text question.title]
    , h5 [  ] [text question.content]
    ]

questionsListView : Model -> Html Msg
questionsListView model =
  ul [ ]
    (model.questions |> List.map (questionView model))

view : Model -> Html Msg
view model =
  div [ ]
    [ h1 [  ] [text "SLACK OVERFLOW"]
    , questionsListView initialModel
    ]

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您的view似乎总是通过initialModel

view : Model -> Html Msg
view model =
  div [ ]
    [ h1 [  ] [text "SLACK OVERFLOW"]
    , questionsListView initialModel
    ]

我想你想把你收到的model作为参数:

view : Model -> Html Msg
view model =
    div [ ]
        [ h1 [  ] [text "SLACK OVERFLOW"]
        , questionsListView model
        ]