-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.fs
98 lines (85 loc) · 2.82 KB
/
Client.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
namespace MySPA
open WebSharper
open WebSharper.JavaScript
open WebSharper.UI
open WebSharper.UI.Client
open WebSharper.UI.Templating
open WebSharper.Sitelets
// The templates are loaded from the DOM, so you just can edit index.html
// and refresh your browser, no need to recompile unless you add or remove holes.
type IndexTemplate = Template<"wwwroot/index.html", ClientLoad.FromDocument>
// Our SPA endpoints
type EndPoint =
| [<EndPoint "/">] Home
| [<EndPoint "/counter">] Counter
[<JavaScript>]
module Pages =
open WebSharper.UI.Notation
open WebSharper.JavaScript
let People =
ListModel.FromSeq [
"John"
"Paul"
]
let HomePage() =
let newName = Var.Create ""
IndexTemplate.HomePage()
// This is where we would instantiate placeholders
// and bind event handlers in our template.
.ListContainer(
People.View.DocSeqCached(fun (name: string) ->
IndexTemplate.ListItem().Name(name).Doc()
)
)
.Name(newName)
.Add(fun _ ->
People.Add(newName.Value)
newName.Value <- ""
)
.Doc()
let storage = JS.Window.LocalStorage
let counter =
let curr = storage.GetItem "counter"
if curr = "" then
0
else
int curr
|> Var.Create
let CounterPage() =
IndexTemplate.CounterPage()
.Value(View.Map string counter.View)
.Decrement(fun e ->
counter := counter.Value - 1
storage.SetItem("counter", string counter.Value)
)
.Increment(fun e ->
counter := counter.Value + 1
storage.SetItem("counter", string counter.Value)
)
.Doc()
[<JavaScript>]
module App =
open WebSharper.UI.Notation
// Create a router for our endpoints
let router = Router.Infer<EndPoint>()
// Install our client-side router and track the current page
let currentPage = Router.InstallHash Home router
type Router<'T when 'T: equality> with
member this.LinkHash (ep: 'T) = "#" + this.Link ep
[<SPAEntryPoint>]
let Main () =
let renderInnerPage (currentPage: Var<EndPoint>) =
currentPage.View.Map (fun endpoint ->
match endpoint with
| Home -> Pages.HomePage()
| Counter -> Pages.CounterPage()
)
|> Doc.EmbedView
IndexTemplate()
.Url_Home(router.LinkHash EndPoint.Home)
.Url_Page1(router.LinkHash EndPoint.Counter)
.TakeMeHome(fun e ->
currentPage := EndPoint.Home
)
.MainContainer(renderInnerPage currentPage)
.Bind()