下面我有一个
button
尝试加载远程内容…
import Post exposing (Post)
import Html exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode
type alias Model =
{ posts : List Post }
type Msg
= Search String
| PostsReceived (Result Http.Error (List Post))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Search s ->
let
cmd =
(Decode.list Post.decode)
|> Http.get ("/posts?author=" ++ s)
|> Http.send PostsReceived
in
( model, cmd )
PostsReceived (Ok posts) ->
{ model | posts = posts }
! []
PostsReceived (Err error) ->
( model, Cmd.none )
view : Model -> Html Msg
view model =
button
[ onClick (Search "amelia") ]
[ text "Read posts by Amelia" ]
这是一个有效的elm程序,只有一个小问题:api不允许我按字符串搜索。这是
不
允许
/posts?author=amelia => Malformed Request Error
然而,这
是
允许
/posts?author=2 => [ {...}, {...}, ... ]
所以我必须
第一
找一个作家来找他/她
id
,和
然后
我可以使用作者的ID来获取帖子…
/author?name=amelia => { id: 2, name: "amelia", ... }
/posts?author=2
我怎样才能把一个请求排在下一个之后呢?理想情况下,我希望将作者缓存在模型中的某个位置,因此我们只请求以前从未见过的作者。