【发布时间】:2016-12-20 20:58:12
【问题描述】:
我通过端口将一组对象传递到我的 Elm 应用程序中。数组中的一个对象的示例是:
{
FullName: 'Foo Bar',
Location: 'Here'
}
您可以看到对象中的键以大写字母开头,因此我需要在 Elm 中对它们进行解码。在我的 Elm 代码中,我有一个 type 用于 Person
type alias Person =
{ fullName : String
, location : String
}
和港口:
port getPeople : (List Json.Decode.Value -> msg) -> Sub msg
最后我有一个解码器(我正在使用Elm Decode Pipeline)将数据解析为Person 类型。
peopleDecoder : Decoder Person
peopleDecoder =
decode Person
|> required "FullName" string
|> required "Location" string
我的问题是如何将传入的端口数据映射到Person 类型?我知道我可以在 JS 中做到这一点,但我宁愿在我的 Elm 代码中做到这一点。
【问题讨论】:
-
在您的示例中
Worker是什么peopleDecoder? -
那是一个错字,现在更正了。
标签: elm