Oh, this is an interesting case. It's tricky because we want a Var<'T> whose value depends both on the current Id and on the contents of the ListModel.

I think this is a good use case for Var.Make which I added recently. It takes a View<'T> and a setter function 'T -> unit, and it returns a Var<'T> whose value comes from this view and which calls this setter when set.

1
2
3
4
5
6
7
8
9
10
let currentPerson =
    let view =
        currentId.View
        |> View.Bind people.TryFindByKeyAsView
        |> View.Map (Option.defaultValue nonPerson)
    let set (p: Person) =
        // Test to avoid adding a new item with id -999!
        if p.Id = currentId.Value then
            people.Add(p)
    Var.Make view set

Unfortunately there seems to be a bug with Var.Make; the above throws an exception on startup because currentPerson is unset. I think I know how it can be fixed, but in the meantime, here's a reimplementation where we pass an explicit initial value: http://try.websharper.com/snippet/loic.denuziere/0000KP

By on 4/3/2018 5:26 AM ()

Wonderful! Var.Make is definitely going to be very useful for multiple cases. Thank you very much for replying!

By on 4/3/2018 5:52 AM ()

The initialization issue is tracked here: https://github.com/dotnet-websharper/ui/issues/167

By on 4/3/2018 6:31 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper