packages feed

happstack-fay-ajax (empty) → 0.2.0

raw patch · 5 files changed

+138/−0 lines, 5 filesdep +fay-basedep +fay-jquerysetup-changed

Dependencies added: fay-base, fay-jquery

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Jeremy Shaw++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Jeremy Shaw nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ happstack-fay-ajax.cabal view
@@ -0,0 +1,27 @@+name:                happstack-fay-ajax+version:             0.2.0+synopsis:            Support for using Fay with Happstack+description:         Fay is strict subset of Happstack which can be compiled+                     to Javascript. This library provides some utilities for client-server RPC.+                     .+                     This package provides the client-side libraries. See also happstack-fay.+homepage:            http://www.happstack.com/+license:             BSD3+license-file:        LICENSE+author:              Jeremy Shaw+maintainer:          jeremy@n-heptane.com+category:            Happstack+build-type:          Simple+cabal-version:       >=1.8++data-files:+  src/AJAX.hs,+  src/ResponseType.hs++library+  hs-source-dirs: src+  exposed-modules:     AJAX,+                       ResponseType++  build-depends:       fay-base         == 0.14.*,+                       fay-jquery       == 0.3.*
+ src/AJAX.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE DeriveDataTypeable, NoImplicitPrelude, PackageImports #-}+{- |++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.++-}+module AJAX where++import "fay-base" Data.Data+import FFI+import JQuery+import ResponseType+import "fay-base" Prelude++-- | 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.+call :: String                    -- ^ URL to 'POST' AJAX request to+     -> (ResponseType res -> cmd) -- ^ AJAX command to send to server+     -> (res -> Fay ())           -- ^ callback function to handle response+     -> Fay ()+call uri f g = ajaxCommand uri (f ResponseType) g++-- | Run the AJAX command. (internal)+--+-- You probably want to use 'call' which provides additional+-- type-safety.+--+-- Note: if the server returns 404 or some other non-success exit+-- code, the callback function will never be run.+--+-- see also: 'call'+ajaxCommand :: String -> Automatic cmd -> (Automatic res -> Fay ()) -> Fay ()+ajaxCommand =+    ffi "jQuery['ajax']({'url': %1, 'type': 'POST', 'data': { 'json' : JSON.stringify(%2) }, 'dataType': 'json', 'success' : %3 })"
+ src/ResponseType.hs view
@@ -0,0 +1,13 @@+{-# LANGUAGE NoImplicitPrelude, DeriveDataTypeable, PackageImports #-}+module ResponseType where++-- NOTE: this module is used by both fay and haskell code. But+-- @fay-base@ uses @ifdefs@ to ensure that each side sees what they+-- need.+import "fay-base" Data.Data+import "fay-base" Prelude++-- | 'ResponseType' is used in lieu of `GADTs` as a mechanism for+-- specifying the expected return type of remote AJAX calls.+data ResponseType a = ResponseType+    deriving (Data, Typeable, Read, Show, Eq)