Created
August 8, 2016 18:38
-
-
Save aph5nt/6f8ce2be2268baa3be0ead5409e1c586 to your computer and use it in GitHub Desktop.
usage of amplify w# binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Application2 | |
open WebSharper | |
open WebSharper.JavaScript | |
open WebSharper.UI.Next | |
open WebSharper.UI.Next.Client | |
open WebSharper.UI.Next.Html | |
open WebSharper.JQuery | |
open WebSharper.Amplify | |
[<JavaScript>] | |
module Client = | |
type Post = { | |
UserId : int | |
Id : int | |
Title : string | |
Body : string | |
} | |
type GetPostbyId = { Id : int } | |
let log msg = JavaScript.Console.Log msg | |
let Main () = | |
/// https://jsonplaceholder.typicode.com/ | |
let getAll() = | |
let settings = new AjaxSettings() | |
settings.Url <- "https://jsonplaceholder.typicode.com/posts" | |
settings.Type <- RequestType.GET | |
settings.DataType <- DataType.Json | |
settings.Cache <- true | |
Amplify.Request.Define("getAllPosts", DefineRequestType.Ajax, settings) // DEFINE NOT FOUND | |
let reqSettings = new RequestSettings("getAllPosts") | |
reqSettings.Success <- (fun(data) -> log data) | |
Amplify.Request(reqSettings) | |
/// https://jsonplaceholder.typicode.com/posts/1 | |
let getById() = | |
let settings = new AjaxSettings() | |
settings.Url <- "https://jsonplaceholder.typicode.com/posts/{id}" | |
settings.Type <- RequestType.GET | |
settings.DataType <- DataType.Json | |
Amplify.Request.Define("getPostById", DefineRequestType.Ajax, settings) // DEFINE NOT FOUND | |
let args = new Object<string>( [|"id", "1"|] ) | |
Amplify.Request( | |
"getPostById", | |
args, | |
(fun(data) -> | |
let data = As<Post> data | |
JavaScript.Console.Log(sprintf "title = %s" data.Title) | |
() | |
)) | |
(* REST OF CODE GOES HERE .... *) | |
let rvInput = Var.Create "" | |
let submit = Submitter.CreateOption rvInput.View | |
let vReversed = | |
submit.View.MapAsync(function | |
| None -> async { return "" } | |
| Some input -> Server.DoSomething input | |
) | |
div [ | |
Doc.Input [] rvInput | |
Doc.Button "Send" [] submit.Trigger | |
hr [] | |
h4Attr [attr.``class`` "text-muted"] [text "The server responded:"] | |
divAttr [attr.``class`` "jumbotron"] [h1 [textView vReversed]] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment