Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created January 28, 2011 10:07
Show Gist options
  • Save t0yv0/800063 to your computer and use it in GitHub Desktop.
Save t0yv0/800063 to your computer and use it in GitHub Desktop.
module Client =
open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Formlet
[<Rpc>]
let CreateBlog blog =
Model.WithBlogs <| fun db ->
db.CreateBlog { blog with Date = DateTime.UtcNow }
[<Rpc>]
let UpdateBlog blog =
Model.WithBlogs <| fun db ->
db.UpdateBlog { blog with Date = DateTime.UtcNow }
[<Inline "document.location = $location">]
let Redirect (location: string) = X<unit>
type BlogControl(homeLocation: string, blog: option<Blog>) =
inherit Web.Control()
new () = new BlogControl("?", None)
[<JavaScript>]
override this.Body =
let (id, title, summary, text) =
match blog with
| None -> (0, "", "", "")
| Some b -> (b.Id, b.Title, b.Summary, b.Text)
let titleForm =
Controls.Input title
|> Enhance.WithTextLabel "Title"
|> Validator.IsNotEmpty "Required."
let summaryForm =
Controls.TextArea summary
|> Enhance.WithTextLabel "Summary"
|> Validator.IsNotEmpty "Summary is required."
let textForm =
Controls.TextArea text
|> Enhance.WithTextLabel "Text"
|> Validator.IsNotEmpty "Text is required"
Formlet.Yield (fun title summary text ->
{
Id = id
Date = DateTime.UtcNow
Title = title
Summary = summary
Text = text
})
<*> titleForm
<*> summaryForm
<*> textForm
|> Enhance.WithLabelConfiguration {
Layout.LabelConfiguration.Default with
VerticalAlign = Layout.VerticalAlign.Top
}
|> Enhance.WithSubmitAndResetButtons
|> Enhance.WithFormContainer
|> Formlet.Run (fun newBlog ->
match blog with
| None -> ignore (CreateBlog newBlog)
| Some _ -> ignore (UpdateBlog newBlog)
Redirect homeLocation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment