(copy of my reply to the same question on StackOverflow)

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:

  • The easy solution is to use Attr.Handler in your CheckBox:

    1
    2
    3
    4
    5
    6
    7
    
    let cbDone =
      Div0 [
        Doc.CheckBox [
          "type" ==> "checkbox"
          Attr.Handler "change" (fun _ -> my'handler())
        ] varDone
      ]
  • The more complex but generally preferable solution is to listen to changes to varDone via a View, rather than listen to events on the element.

    Now, the way UI.Next works, only views that connect into the sink (ie. the rendered document) are actually computed, so nothing will happen if you only do something like this:

    1
    
    let _ = varDone.View |> View.Map (fun _ -> my'handler())

    Instead, the result value of the above expression (which has type View<unit>) needs to be connected to the returned document. You can use the following helper (which we'll probably add to UI.Next in some form soon):

    1
    2
    3
    4
    5
    
    let AddViewListener (view: View<'T>) (f: 'T -> unit) (doc: Doc) =
      view
      |> View.Map (fun x -> f x; Doc.Empty)
      |> Doc.EmbedView
      |> Doc.Append doc

    which you can use as such:

    1
    2
    3
    
    let cbDone =
      Div0 [ Doc.CheckBox [ "type" ==> "checkbox" ] varDone ]
      |> AddViewListener varDone.View (fun _ -> my'handler())

    The above code basically means "as long as cbDone is in the rendered document, any change to varDone will trigger my'handler".

By on 6/15/2015 6:57 AM ()

thank you very much, especially for the second solution. Yes, I have given does not fully correct the pseudo code. Of course, I added and was looking in the debugger event listener on chekbox, not Div0: ''' let cbDone = Doc.CheckBox "type" ==> "checkbox" varDone '''

By on 6/16/2015 9:31 PM ()

Another solution is to use UI.Next templates and bind to the change event on the checkbox.

By on 6/15/2015 12:14 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper