The recommended approach is to rewrite the logic you need into Elm.
Otherwise, what you can do is make your model store the current money,
then pull it back in through a port. Store the input money, as a
placeholder until it has been calculated and recieved back into Elm.
It should look something like this:
type Money = Unformatted String | Formatted String
type alias Model = { currentMoney : Money }
type Msg = OnMoneyInput String | FormattedMoney String
update msg model =
case msg of
OnMoneyInput money -> ({ model | currentMoney = Unformatted money
}, Format.toMoney money)
FormattedMoney money -> ( { model | currentMoney = Formatted
String }, Cmd.none )
subs model =
Format.incomingMoney FormattedMoney
view model =
case model.currentMoney of
Unformatted money -> Html.text money
FormattedMoney money -> Html.text money
main = ...
On Sat, Apr 29, 2017 at 2:26 PM, Dwayne Crooks <[email protected]> wrote:
> I'm porting an application from React/Redux to Elm and I'm having trouble
> figuring out how to format a value as money. In the original application we
> used http://openexchangerates.github.io/accounting.js/. So naturally I
> wanted to make use of that same library when writing the Elm version. Based
> on my reading ports seem to be the solution however when I think through the
> implications it doesn't seem natural to write my code in that way when
> formatting is a presentation concern.
>
> Here's what I came up with:
>
> 1. I created a port module called Format.
>
>> port module Format exposing (..)
>>
>> port asMoney : Float -> Cmd msg
>> port moneyFormats : (String -> msg) -> Sub msg
>
>
> 2. I originally envisioned writing the view as follows:
>
>> viewSales : Float -> Html Msg
>> viewSales amount =
>> viewWidget "Gross Sales (All Time)" (Format.asMoney amount)
>
>
> But obviously, that's out the window since Format.asMoney returns a command.
>
> 3. It means I now have to format in my update function and store the
> formatted data in my model so that my view can access it. I find that very
> inconvenient and I don't want presentation concerns in neither my update
> function nor my model.
>
> Am I thinking through this correctly?
>
> How do I proceed?
>
> Should I consider writing an external library with a Native module like for
> e.g. https://github.com/NoRedInk/elm-moment or
> https://github.com/evancz/elm-graphics?
>
> Any help would be greatly appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.