webf#websharper

Doc.Checkbox 'change' event does not occur in Websharper.UI.Next


I have reactive Var variable varDone and Doc.Checkbox for its representation named cbDoc. After changing the value of varDone I need to call my function. For this, I wrote the code illustrated in the following pseudo-code:

open WebSharper
open WebSharper.UI.Next
open WebSharper.UI.Next.Html
open WebSharper.UI.Next.Notation

// declaring reactive bool variable
let varDone = Var.Create false

(* something else *)

// creting Doc-representation of my model
let renderModel model = 

    // declaring its representation Doc element
    let cbDone = Div0 [ Doc.CheckBox [ "type" ==> "checkbox" ] varDone ]

    let result'doc = // a Doc-representation, that contains cbDone

    let my'handler()  : unit -> unit = // a function that should be called when changing the value of varDone

    // add an event handler for the element cbDone changes
    (Doc.AsPagelet cbDone).Body.AddEventListener( "change", my'handler, false )

    result'doc

But unfortunately no event occurs when checkbox changing. The question is, what am I doing wrong and is there another way to respond to changes in value of varDone variable?

UPD

The saddest thing is that an element with checkbox does not even have my event handler. This is evident when debug in browser.


Solution

  • Edit: This is how the second solution below would be implemented with current WebSharper.UI:

    let cbDone =
        div [] [
            Doc.CheckBox [
                attr.``type`` "checkbox"
                on.viewUpdate varDone.View (fun _ _ -> my'handler())
            ] varDone
        ]
    

    What happens is that Doc.AsPagelet adds a wrapper around the doc, so you are adding an event listener to that wrapper. And even if it did not, you would be adding an event listener to the Div0 you created instead of the CheckBox itself.

    That being said, there are two solutions here: