榆木,如何获取当前时间并将其发布到服务器 [英] Elm, how to get current time and post it to server
问题描述
根据此答案和其有用的Ellie 现在,我对如何获得Elm 0.18的当前时间有所了解。
According to this answer and its helpful Ellie I now have an idea of how we get the current time in Elm 0.18.
我想获取当前时间,然后将其以JSON格式发布到我的服务器。我已经有一个使用硬编码时间戳的POST,但我只是看不到如何获取当前时间,然后将其包含在我正在发布的JSON中。
我的猜测是我需要链接几个命令(获取当前时间,向服务器发送POST)。
My guess is that I need to chain a couple of commands (get current time, make a POST to server).
[我也读过另一个SO ,其中显示了如何通过直接调用 update $来连续运行一些命令c $ c>函数,听起来不错,但是我不确定是否适合我的情况。]
[I have also read another SO which shows how you can run a few commands in a row by directly calling the update
function, which sounds like a nice idea but I am not sure if it is what I need for my situation.]
为了解决这个问题,我想我会尝试解决一个类似的问题,我可以更轻松地在Ellie中进行设置。
In order to get my head around this I thought I would try to solve a similar problem that I can more easily set up in Ellie.
在原始Ellie 中被获取,更新到模型中,然后查看功能n显示出来。
In the original Ellie the current time is gotten, updated into the model, and then the view function shows it.
在我的版本我希望这是一个分为两个步骤的过程,因此现在将该模型扩展为 Maybe Float
和 String
消息。 view
函数显示消息字符串和一个按钮-计划是,当按下按钮时,它告诉运行时获取当前时间,然后将其复制
In my version I wanted this to be a two-step process and therefore have grown the model to be a Maybe Float
for the time and a String
message. The view
function shows the message string and a button -- the plan is that when the button is pressed it tells the runtime to 'go get the current time and then copy it across into the message slot'.
如果我可以解决这个问题,那么我感觉可以解决我原来的问题。
If I can solve this problem then I feel like I can solve my original problem.
我的Ellie尚未执行此操作。当您按下按钮时,时间已获取并放入模型
的 time
插槽中,但是我没有知道如何告诉运行时 ...现在将该时间复制到消息槽中。 PutTimeInMessage
消息已经到位,但是我不知道如何在 GetCurrentTime
消息/之后运行它
My Ellie does not do this yet. When you press the button the time is gotten and is put into the time
slot in the model
, but I do not know how to tell the runtime to '...now copy that time across into the message slot'. The PutTimeInMessage
message is in place, but I don't know how to get it to run after the GetCurrentTime
message/command.
有什么建议吗?
到目前为止,这是我的代码(可以编译),您可以< a href = https://ellie-app.com/XZ7NPq9NPxa1 rel = nofollow noreferrer>在Ellie中运行:
Here is my code so far (it compiles), which you can run here in Ellie:
module Main exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import Time exposing (Time)
import Date
import Task
type alias Model =
{ time : Maybe Float
, message : String
}
type Msg
= OnTime Time
| GetCurrentTime
| PutTimeInMessage
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnTime t ->
( { model | time = Just t }, Cmd.none )
GetCurrentTime ->
( model, getTime )
PutTimeInMessage ->
case model.time of
Nothing ->
( model, Cmd.none )
Just t ->
( { model | message = toString t }, Cmd.none )
view : Model -> Html Msg
view model =
div []
[ div []
[ button [ onClick GetCurrentTime ] [ Html.text "Get now time." ]
]
, model.message |> Html.text
]
getTime : Cmd Msg
getTime =
Time.now
|> Task.perform OnTime
main =
Html.program
{ init = ( Model Nothing "Empty message.", Cmd.none )
, update = update
, view = view
, subscriptions = always Sub.none
}
推荐答案
我遇到了 SO,它解释了如何执行Http序列 Task.andThen
请求。因为我可以看到 Time.now
的类型是 Task
的类型,所以我认为可以根据自己的目的修改该示例如果我使用 Http.toTask
。
I came across an SO which explained how to do a sequence of Http requests with Task.andThen
. Since I can see the type of Time.now
is a Task
I figured that I could adapt that example for my purposes if I use Http.toTask
.
下面是我想出的解决方案,这是在Ellie中:
Below is the solution I came up with and here it is in Ellie:
module Main exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as JD
import Json.Encode as JE
import Task
import Time
type alias Model =
{ url : String
}
type Msg
= PostTimeToServer
| PostDone (Result Http.Error String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PostTimeToServer ->
( model, postTimeToServer model.url )
PostDone _ ->
( model, Cmd.none )
view : Model -> Html Msg
view model =
div []
[ div []
[ button [ onClick PostTimeToServer ] [ Html.text "POST the current time." ]
]
]
postTimeToServer : String -> Cmd Msg
postTimeToServer url =
let
getTime =
Time.now
postTime t =
JD.string
|> Http.post url (JE.float t |> Http.jsonBody)
|> Http.toTask
request =
getTime <<-- Here is
|> Task.andThen postTime <<-- the key bit.
in
Task.attempt PostDone request
main =
Html.program
{ init = ( Model "url_here", Cmd.none )
, update = update
, view = view
, subscriptions = always Sub.none
}
这篇关于榆木,如何获取当前时间并将其发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!