Skip to content

Commit 9a19c95

Browse files
author
Tyson Williams
committed
now have composable bindings in SubModelSeq sample
1 parent 0959c44 commit 9a19c95

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/Samples/SubModelSeq/Program.fs

+42-34
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ module CounterTypes =
8989
| SetStepSize x -> { m with StepSize = x }
9090
| Reset -> init
9191

92+
let bindings () : Binding<Counter, CounterMsg> list = [
93+
"CounterValue" |> Binding.oneWay (fun m -> m.Count)
94+
"Increment" |> Binding.cmd Increment
95+
"Decrement" |> Binding.cmd Decrement
96+
"StepSize" |> Binding.twoWay(
97+
(fun m -> float m.StepSize),
98+
int >> SetStepSize)
99+
"Reset" |> Binding.cmdIf(Reset, canReset)
100+
]
92101

93102

94103
[<AutoOpen>]
@@ -212,40 +221,39 @@ module Bindings =
212221

213222
open App
214223

215-
let rec counterTreeBindings () : Binding<Model * Identifiable<Counter>, Msg> list = [
216-
"CounterIdText" |> Binding.oneWay(fun (_, c) -> c.Id)
217-
218-
"CounterValue" |> Binding.oneWay(fun (_, c) -> c.Value.Count)
219-
"Increment" |> Binding.cmd(fun (_, c) -> CounterMsg (c.Id, Increment))
220-
"Decrement" |> Binding.cmd(fun (_, c) -> CounterMsg (c.Id, Decrement))
221-
"StepSize" |> Binding.twoWay(
222-
(fun (_, c) -> float c.Value.StepSize),
223-
(fun v (_, c) -> CounterMsg (c.Id, SetStepSize (int v))))
224-
"Reset" |> Binding.cmdIf(
225-
(fun (_, c) -> CounterMsg (c.Id, Reset)),
226-
(fun (_, c) -> Counter.canReset c.Value))
227-
228-
"Remove" |> Binding.cmd(fun (_, c) -> Remove c.Id)
229-
230-
"AddChild" |> Binding.cmd(fun (_, c) -> AddCounter c.Id)
231-
232-
"MoveUp" |> Binding.cmdIf(
233-
(fun (_, c) -> MoveUp c.Id),
234-
(fun (m, c) -> m |> childrenCountersOfParentOf c.Id |> List.tryHead <> Some c))
235-
236-
"MoveDown" |> Binding.cmdIf(
237-
(fun (_, c) -> MoveDown c.Id),
238-
(fun (m, c) -> m |> childrenCountersOfParentOf c.Id |> List.tryLast <> Some c))
239-
240-
"GlobalState" |> Binding.oneWay(fun (m, _) -> m.SomeGlobalState)
241-
242-
"ChildCounters" |> Binding.subModelSeq(
243-
(fun (m, c) -> m |> childrenCountersOf c.Id),
244-
(fun ((m, _), childCounter) -> (m, childCounter)),
245-
(fun (_, c) -> c.Id),
246-
snd,
247-
counterTreeBindings)
248-
]
224+
let rec counterTreeBindings () : Binding<Model * Identifiable<Counter>, Msg> list =
225+
let counterBindings =
226+
Counter.bindings ()
227+
|> Bindings.mapModel (fun (_, c) -> c.Value)
228+
|> Bindings.mapMsgWithModel (fun (_, c) msg -> CounterMsg (c.Id, msg))
229+
let newBindings =
230+
[
231+
"CounterIdText" |> Binding.oneWay(fun (_, c) -> c.Id)
232+
233+
"Remove" |> Binding.cmd(fun (_, c) -> Remove c.Id)
234+
235+
"AddChild" |> Binding.cmd(fun (_, c) -> AddCounter c.Id)
236+
237+
"MoveUp" |> Binding.cmdIf(
238+
(fun (_, c) -> MoveUp c.Id),
239+
(fun (m, c) -> m |> childrenCountersOfParentOf c.Id |> List.tryHead <> Some c))
240+
241+
"MoveDown" |> Binding.cmdIf(
242+
(fun (_, c) -> MoveDown c.Id),
243+
(fun (m, c) -> m |> childrenCountersOfParentOf c.Id |> List.tryLast <> Some c))
244+
245+
"GlobalState" |> Binding.oneWay(fun (m, _) -> m.SomeGlobalState)
246+
247+
"ChildCounters" |> Binding.subModelSeq(
248+
(fun (m, c) -> m |> childrenCountersOf c.Id),
249+
(fun ((m, _), childCounter) -> (m, childCounter)),
250+
(fun (_, c) -> c.Id),
251+
snd,
252+
counterTreeBindings)
253+
]
254+
[ counterBindings
255+
newBindings ]
256+
|> List.concat
249257

250258

251259
let rootBindings () : Binding<Model, Msg> list = [

0 commit comments

Comments
 (0)