除了在JSON事件中的isTrusted之外,Elm没有数据

时间:2016-11-21 18:56:23

标签: elm

我写了一个基于time example的简单程序来测试事件中的数据。它将JSON解码为value,然后将其编码回JSON,然后在SVG文本元素中显示它。我唯一得到的是{"isTrusted":true}

为什么会这样?如何获取其他事件数据?我正在使用Firefox 49和online compiler

import Html exposing (Html)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Svg.Events exposing(on)
import Json.Decode as Json
import Json.Encode exposing (encode)

main =
  Html.program
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

-- MODEL
type alias Model = String

init : (Model, Cmd Msg)
init =
  ("No event", Cmd.none)


-- UPDATE
type Msg
  = Event String

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Event event ->
      (event, Cmd.none)


subscriptions model = Sub.none


stringifyEvent: Json.Encode.Value -> Msg
stringifyEvent x = 
  Event (encode 2 x)

-- VIEW

view : Model -> Svg Msg
view model =
    svg [ viewBox "0 0 300 300", width "300px", on "mousedown" (Json.map stringifyEvent Json.value) ] [ 
       text_ [x "0", y "30"] [text model]
    ]

当我在控制台中尝试时

svgElement.addEventListener('click', function(e) {console.log(e)})

它适用于所有属性。

1 个答案:

答案 0 :(得分:3)

我不知道如何实现目标。 但我可以给你一个答案,说明为什么它按照你描述的方式进行。

如果查看源代码,您会发现Elm运行时使用 JSON.stringify()用于将Value转换为String

猜猜是什么...... svgElement.addEventListener('click', function(e) {console.log(JSON.stringify(e))}) 点击后会给你{"isTrusted":true}