WebSharper

I am maybe missing some api funcaitonality, but I can't seem to figure out how to bind an attribute to a control using a Var. To be clear, I'm looking to do the equivalent of Controls.Input v -< [WebSharper.UI.Next.Client.Attr.Dynamic "data-list" var.View]. So the data-list would be updated when the server updates the value of var. Is this possible without refactoring all of my current piglet rendering functions to the Doc.Input api varient?

  • loic.denuziere

    Do I understand this correctly: you're using WebSharper.Piglets, and WebSharper.Html elements, in conjunction with UI.Next Vars? It really wasn't designed to be used this way :/ The Piglets implementation for UI.Next is WebSharper.Forms, and more generally it is really recommended to use Vars with Docs, as those are designed to avoid the sink problem below.

    In your current situation you could do something like this:

    1
    2
    3
    4
    5
    6
    
    let attrDyn attrName view e =
        view |> View.Sink (fun v -> e.SetAttribute(attrName, v))
    
    // ...
    
    Controls.Input v |>! attrDyn "data-list" var.View

    But you have to be careful with View.Sink: if the above is an element you insert a set number of times into your document, then it's all right, but if it's a snippet that gets inserted repeatedly (for example if it's in a Piglet.Many) then you're exposing yourself to memory leaks (which I'll admit is kind of ironic for a Sink...).

    • TPierson

      Thanks for the reply, that is clarifying. I've been using Piglets in conjunction with Websharper.Html elements as per https://github.com/intellifactory/websharper.piglets/blob/master/WebSharper.Piglets.Test/App.fs. I didn't know about WebSharper.Forms (or if I did, assumed it was superceded by UI.Next or Formlets). Is there some documentation somewhere about the tradoffs between Piglets + WebSharper.Html and WebSharper.Forms? Should I just prefer the latter? In general I've found the documentation to be quite vague about the relation (in terms of design, interoperability, current best-practices, and legacy-support modules) between the various Websharper modules.