Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary PR to show minor binding issue #2

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions src/Samples/SingleCounter/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,52 @@ open System
open Elmish
open Elmish.WPF

type Model =
{ Count: int
StepSize: int }

let init () =
{ Count = 0
StepSize = 1 }

type Msg =
| Increment
| Decrement
| SetStepSize of int
| Reset

let update msg m =
match msg with
| Increment -> { m with Count = m.Count + m.StepSize }
| Decrement -> { m with Count = m.Count - m.StepSize }
| SetStepSize x -> { m with StepSize = x }
| Reset -> init ()

let bindings () : Binding<Model, Msg> list = [
"CounterValue" |> Binding.oneWay (fun m -> m.Count)
"Increment" |> Binding.cmd Increment
"Decrement" |> Binding.cmd Decrement
"StepSize" |> Binding.twoWay(
(fun m -> float m.StepSize),
int >> SetStepSize)
"Reset" |> Binding.cmdIf(Reset, (<>) (init ()))
]
module MainModule =

type Model =
{ Count: int
StepSize: int }

let init () =
{ Count = 0
StepSize = 1 }

type Msg =
| Increment
| Decrement
| SetStepSize of int
| Reset

let update msg m =
match msg with
| Increment -> { m with Count = m.Count + m.StepSize }
| Decrement -> { m with Count = m.Count - m.StepSize }
| SetStepSize x -> { m with StepSize = x }
| Reset -> init ()


module BindingModule =

let notOverloadedTwoWay
(get: 'model -> 'a,
set: 'a -> 'msg)
: string -> Binding<'model, 'msg> =
Binding.twoWay(get, set)

let bindings () : Binding<MainModule.Model, MainModule.Msg> list = [
"CounterValue" |> Binding.oneWay (fun m -> m.Count)
"Increment" |> Binding.cmd MainModule.Increment
"Decrement" |> Binding.cmd MainModule.Decrement
"StepSize" |> notOverloadedTwoWay(
(fun m -> float m.StepSize),
int >> MainModule.SetStepSize)
"Reset" |> Binding.cmdIf(MainModule.Reset, (<>) (MainModule.init ()))
]


[<EntryPoint; STAThread>]
let main argv =
Program.mkSimpleWpf init update bindings
Program.mkSimpleWpf MainModule.init MainModule.update BindingModule.bindings
|> Program.withConsoleTrace
|> Program.runWindowWithConfig
{ ElmConfig.Default with LogConsole = true; Measure = true }
Expand Down