Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
websharper jquery listeners in client-server templates
namespace Testest
open WebSharper
open WebSharper.JavaScript
open WebSharper.UI.Next
open WebSharper.UI.Next.Client
open WebSharper.UI.Next.Html
open WebSharper.JavaScript
open WebSharper.JQuery
module Client =
[<Direct "new Awesomplete($input)">]
let awesomplete input = X<unit>
type JQuery with
[<Inline "$this.select2()">]
member this.Select2() = X<JQuery>
// Workaround for
[<Inline "new Event($name)">]
let NewEvent (name: string) = X<Dom.Event>
let personName = Var.Create "Bob"
let personName2 = Var.Create "Abe"
let Main () =
let cls s = attr.``class`` s
let data a b = attr.``data-`` a b
let opts = Var.Create (Some [""; "Abe"; "Andrew"; "Bill"; "Bob"; "Joe"; "Rex"])
div [
div [
h4 [text "Awesomplete"]
div [
[ "auto-complete"
data "list" "Abe,Andrew,Bill,Bob,Joe,Rex"
data "minchars" "0"
on.afterRender (fun el ->
.On("awesomplete-selectcomplete", fun el ev ->
el.DispatchEvent(NewEvent("change")) |> ignore)
awesomplete el)
p [text "This input works correctly when typing - text is reactively updated below - but fails when selecting from the autocomplete options."]
hr []
h4 [text "You entered"]
div [
textView personName.View
br []
br []
br []
br []
hr []
h4 [text "Select2"]
div [
|> View.Map (fun op -> match op with
| Some o ->
Doc.Select [
on.afterRender (fun e ->
.Change(fun el ev ->
if not (JS.HasOwnProperty ev "originalEvent" && ev.OriginalEvent?uinext) then
let domEv = NewEvent("change")
domEv?uinext <- true
el.DispatchEvent(domEv) |> ignore)
] id o personName2
| None -> div []
) |> Doc.EmbedView
hr []
h4 [text "You entered"]
div [
textView personName2.View
namespace Testest
open WebSharper
open WebSharper.Sitelets
open WebSharper.UI.Next
open WebSharper.UI.Next.Server
type EndPoint =
| [<EndPoint "/">] Home
| [<EndPoint "/about">] About
module Templating =
open WebSharper.UI.Next.Html
type MainTemplate = Templating.Template<"Main.html">
// Compute a menubar where the menu item for the given endpoint is active
let MenuBar (ctx: Context<EndPoint>) endpoint : Doc list =
let ( => ) txt act =
liAttr [if endpoint = act then yield attr.``class`` "active"] [
aAttr [attr.href (ctx.Link act)] [text txt]
li ["Home" => EndPoint.Home]
li ["About" => EndPoint.About]
let Main ctx action title body =
title = title,
menubar = MenuBar ctx action,
body = body
module Site =
open WebSharper.UI.Next.Html
let HomePage ctx =
Templating.Main ctx EndPoint.Home "Home" [
h1 [text "Say Hi to the server!"]
div [client <@ Client.Main () @>]
let AboutPage ctx =
Templating.Main ctx EndPoint.About "About" [
h1 [text "About"]
p [text "This is a template WebSharper client-server application."]
let Main =
Application.MultiPage (fun ctx endpoint ->
match endpoint with
| EndPoint.Home -> HomePage ctx
| EndPoint.About -> AboutPage ctx
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="" />
/* Sticky footer styles */
html {
position: relative;
min-height: 100%;
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
.footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
background-color: #f5f5f5;
.container .text-muted {
margin: 20px 0;
<!--<link rel="stylesheet" href="" />-->
<link rel="stylesheet" href="" />
<link href="" rel="stylesheet" />
<!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<a class="navbar-brand" href="#">Your App</a>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav" data-hole="menubar"></ul>
<ul class="nav navbar-nav navbar-right">
<li><a href=""></a></li>
</div><!--/.nav-collapse -->
<div class="container">
<div data-replace="body">
<footer class="footer">
<div class="container">
<p class="text-muted">
For an enhanced template that provides automatic GitHub deployment to Azure, fork from <a href="">GitHub</a>, or
read more <a href="">here</a>.
<script data-replace="scripts"></script>
<script src=""></script>
<script src=""></script>
<add key="WebSharper.JQuery.Resources.JQuery" value="" />
<!-- NOTE: comment the following to run on F# 3.0 -->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="" newVersion="" />
<!-- NOTE: remove debug="true" to serve compressed JavaScript -->
<compilation debug="true" targetFramework="4.0" />
<!-- This is only needed for VS Development WebServer. IIS/IIS Express do not use this:-->
<add name="WebSharper.RemotingModule" type="WebSharper.Web.RpcModule, WebSharper.Web" />
<add name="WebSharper.Sitelets" type="WebSharper.Sitelets.HttpModule, WebSharper.Sitelets" />
<add name="WebSharper.RemotingModule" type="WebSharper.Web.RpcModule, WebSharper.Web" />
<add name="WebSharper.Sitelets" type="WebSharper.Sitelets.HttpModule, WebSharper.Sitelets" />
<!-- This is only needed for VS Development WebServer (see above). IIS/IIS Express do not use this: -->
<validation validateIntegratedModeConfiguration="false"/>

o1lo01ol1o commented Feb 19, 2017

Jquery listeners are not properly triggered when the page is loaded; in particular '$this. Select2()' fails which seems to point to either the select2 library being ubound by susbsequent imports or never being instantiated in t the first place. however, the fact that the awesomeplete code works but does not update the Var seems to suggest that the on.afterRender handlers are not being called correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment