elm

How to convert from String to Int in Json.Decoder

吃可爱长大的小学妹 提交于 2019-12-07 01:30:50
问题 Here's my decoder: decodeData : Json.Decoder (Id, String) decodeData = Json.at ["data", "0"] <| Json.object2 (,) ("id" := Json.int) ("label" := Json.string) The id should logically be Int however my backend sends it as String (e.g. we get "1" instead of 1 ). How can I cast the decoded value to Int ? 回答1: ... and to answer myself :) I found the solution in this Flickr example decodeData : Json.Decoder (Id, String) decodeData = let number = Json.oneOf [ Json.int, Json.customDecoder Json.string

Elm: JSON decoder, convert String to Bool

我只是一个虾纸丫 提交于 2019-12-06 13:09:41
I'm receiving a JSON that looks like this: { name: "NAME1", value: "true" } I would like to create a json decoder that would create a record like this: { name: "NAME1", value: True } I'm trying to make a decoder that transforms "true" into True. I did this so far: userProperties : Json.Decode.Decoder Props userProperties = Json.Decode.object2 (,) ("name" := Json.Decode.string) ("value" := Json.Decode.string) `andThen` \val -> let newVal = -- Do something here? in Json.Decode.succeed <| newVal There are a few problems in your example so let's step through each of them. You haven't shown the

Calling window.open through port

心已入冬 提交于 2019-12-06 11:13:26
I'm implementing social auth. When the user clicks on a button, I send a command so I can call window.open() . Looking at this call-stack, we can see that the port handler is called in the next event loop: Since window.open is not being called within the click event lifecycle, browsers like safari do not allow for the popup to show up. What is your approach? It's not super pretty, but you can do something like a [ Html.Attributes.attribute "onClick" "window.open(this.href, this.target, 'width=800,height=600'); return false;" ] [ text "Click me" ] It's an underhanded way to handle this, and not

ELM/Haskell : use Regular expression(Regex) to search a String and render Html view

做~自己de王妃 提交于 2019-12-06 09:03:40
问题 what I am trying to do is to parse string using regular expression and get Html element as output so the function signature should be as following: parse : String -> Html Msg before we dive in the code let us take an example how the code should behave in order to have a clear idea, given a the below string as below : input : hello (!BOLD!) I Am a bold text (!BOLD!)bla bla la expected output : div [ ][ text "hello", b [ ][ text "I Am a bold text" ] , text "bla la la"] ] in order to achieve

Parsing nested JSON in Elm

痴心易碎 提交于 2019-12-06 04:42:16
问题 I have this situation -- this is in post.elm type alias Model = { img : String , text : String , source : String , date : String , comments : Comments.Model } -- this is in comments.elm type alias Model = List Comment.Model -- this is in comment.elm type alias Model = { text : String , date : String } I am trying to parse a JSON so formed { "posts": [{ "img": "img 1", "text": "text 1", "source": "source 1", "date": "date 1", "comments": [{ "text": "comment text 1 1", "date": "comment date 1 1

In Elm, is there a way to merge union types ? (for modularity purpose)

江枫思渺然 提交于 2019-12-06 03:43:21
问题 Starting from those three declarations : type SharedMsg = SharedAction type Page1Msg = Page1Action type Page2Msg = Page2Action I there a way to obtain an equivalent of the following one? Like a way to "merge" union types ? type Msg = SharedAction | Page1Action | Page2Action ============================= Context : I am splitting an Elm application into one module per page with their own folders. Some actions will be shared, and some actions will be page-specific. If I was to use the Html.map

cabal sandbox install still fails with “packages are likely to be broken by the reinstalls”

筅森魡賤 提交于 2019-12-06 02:19:26
问题 I'm trying to build the Elm Platform using a cabal sandbox. The installation fails however with the message "packages are likely to be broken by the reinstalls". This seems to me to contradict the whole purpose of sandboxing, which I had believed was supposed to avoid this kind of error. Can anybody explain this error for me, and even better help me proceed with the Elm install? Is it safe to add --force-reinstalls ? Here are the sequence of commands I executed: cabal sandbox init cabal

Elm: adding click events to SVG elements doesn't work – is this possible?

北慕城南 提交于 2019-12-05 17:27:54
I have been attempting to add an on "click" event to an SVG element in Elm in order to determine the relative position of the mouse click within that element. A code sample is given below that you can try running at http://elm-lang.org/try to show how click events on HTML elements seem to work as expected but not on SVG elements. In the sample, Html.on "click" is used rather than Html.onClick to allow the position data to be decoded from the event as explained in this discussion . After reading the documentation and the source code, I would expect that when the on "click" event is added to an

Elm: Json decoder timestamp to Date

荒凉一梦 提交于 2019-12-05 14:46:33
问题 I'm trying to convert a timestamp (ex: "1493287973015") from a JSON to a Date type. So far I created this custom decoder: stringToDate : Decoder String -> Decoder Date stringToDate decoder = customDecoder decoder Date.fromTime But it doesn't work because it has return a Result, not a Date: Function `customDecoder` is expecting the 2nd argument to be: Time.Time -> Result String a But it is: Time.Time -> Date.Date Is there a way to do a conversion? 回答1: Assuming your JSON is actually placing

How can I detect failure to load an image in Elm

ぐ巨炮叔叔 提交于 2019-12-05 14:35:22
How can I detect that an image was failed to be load in Elm? I use img [ src "/path/to/image" ] and would like to know if the image failed to load. In plan old JavaScript I would register to onError event of img but I don't see onError in Elm. You can use Html.Events.on to capture an image load error. You will need a Msg value to indicate that the image failed. Here I'll call that ImageError : img [ src "/path/to/image", on "error" (Json.Decode.succeed ImageError) ] [] The use of Json.Decode.succeed may look confusing in this context (after all, you're trying to capture an error, so what is