Safe Haskell | None |
---|
client-side half of a typed AJAX communication channel.
To use this library, you could start by defining a type in a file that can be shared between the Haskell Server and Fay client. For example:
data Command = SendGuess Guess (ResponseType (Maybe Row)) | FetchBoard (ResponseType (Maybe Board)) deriving (Read, Show, Data, Typeable) instance Foreign Command
The ResponseType
argument specifies what type each command should
return. Using GADTs would be cleaner, but Fay does not support GADTs
yet.
To execute a remote function we use the call
function:
call /ajax FetchBoard $ mboard -> ...
Due to the single-threaded nature of Javascript, we do not want to
block until the call
returns a value, so we perform the AJAX request
asynchronously. The third argument to call
is the callback function
to run when the response is received.
Documentation
:: String | URL to |
-> (ResponseType res -> cmd) | AJAX command to send to server |
-> (res -> Fay ()) | callback function to handle response |
-> Fay () |
Asynchronously call a command
Note: if the server returns 404 or some other non-success exit code, the callback function will never be run.
This function is just a wrapper around ajaxCommand
which uses the
'ResponseType res' phantom-typed parameter for added type safety.