Reputation: 4465
I'm trying to initialize my model with the current date. I don't want to use a Native module. I'm trying to do it with Task and Effects. I'm stuck at my getCurrentTime
method. What is the cleanest way?
import Time exposing ( Time )
import StartApp
import Task
import Effects exposing (Never)
import Html exposing ( Html )
app =
StartApp.start
{ init = init
, view = view
, update = update
, inputs = []}
main =
app.html
port tasks : Signal (Task.Task Never ())
port tasks =
app.tasks
type alias Model =
{ clock : Time}
init : (Model, Effects.Effects Action)
init = ((Model ), Effects.none)
type Action = ShowClock
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case action of
ShowClock c ->
({ model | clock = c}, Effects.none)
getCurrentTime : Effects.Effects Action
getCurrentTime =
-- stuck here
|> Task.map ShowClock
|> Effects.task
view : Signal.Address Action -> Model -> Html
view address model =
Signal.map Html.text model.clock
It would also be nice to convert the time to a string with "YYYY-MM-DD" format.
Upvotes: 4
Views: 631
Reputation: 307
There is an ELM package available at http://package.elm-lang.org/packages/evancz/task-tutorial/1.0.3/TaskTutorial you can use to get the current time.
First you need to download it and update your elm-package.json file using the following command in a terminal or Windows command:
elm-package install evancz/task-tutorial 1.0.3
Then update your code :
getCurrentTime : Effects.Effects Action
getCurrentTime = Effects.task <| Task.map ShowClock ( TaskTutorial.getCurrentTime )
Finally, you just have to call getCurrentTime as an Effects Action and your model will be updated.
To convert your Time into a human readable date String you can use this little helper function:
toHRDate : Time -> String -- to human readable date
toHRDate t =
let
date = Date.fromTime t
d = toString (Date.day date) -- day
m = toString (Date.month date) -- month
y = toString (Date.year date) -- year
hrd = d ++ "-" ++ m ++ "-" ++ y
in hrd
Upvotes: 2
Reputation: 366
If we simply want to pass an initial value at the beginning of your program, we can use a port:
So, we'd add this to the application that contains main:
port time : Float
...and then we have to add the following to our html to give our elm program the correct initial value.
var myapp = Elm.fullscreen(Elm.YourAppNameHere,
{
time: Date.now()
});
The interop documentation on the elm language website covers this reasonably and has links to several other examples: http://elm-lang.org/guide/interop
Alternatively, you could try out foldp'
from the Signal.Extra library, which exposes initial values a little more explicitly. http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/5.7.0
Upvotes: 1