From 99215cd6548e83880bb288061971afe60ac38e88 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Mon, 1 Dec 2014 17:36:18 +0100 Subject: [PATCH] first shot at generating function names automatically --- examples/counter.hs | 8 ++------ examples/www/api.js | 11 +++++------ examples/www/index.html | 6 +++--- src/Servant/JQuery/Internal.hs | 31 ++++++++++++++++--------------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/examples/counter.hs b/examples/counter.hs index 87b4d43e..d5b6b156 100644 --- a/examples/counter.hs +++ b/examples/counter.hs @@ -62,10 +62,8 @@ runServer :: TVar Counter -- ^ shared variable for the counter runServer var port = run port (serve testApi $ server var) -- * Generating the JQuery code -incCounterNamed :: FunctionName -> AjaxReq -currentValueNamed :: FunctionName -> AjaxReq -incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi +incCounterJS :<|> currentValueJS :<|> _ = jquery testApi writeJS :: FilePath -> [AjaxReq] -> IO () writeJS fp functions = writeFile fp $ @@ -75,9 +73,7 @@ main :: IO () main = do -- write the JS code to www/api.js at startup writeJS (www "api.js") - [ incCounterNamed "increaseCounter" - , currentValueNamed "getCurrentValue" - ] + [ incCounterJS, currentValueJS ] -- setup a shared counter cnt <- newCounter diff --git a/examples/www/api.js b/examples/www/api.js index 4464105f..0adbd89a 100644 --- a/examples/www/api.js +++ b/examples/www/api.js @@ -1,21 +1,20 @@ -function increaseCounter(onSuccess, onError) +function postcounter(onSuccess, onError) { $.ajax( { url: '/counter' - , success: onSuccess + , success: onSuccess , error: onError , type: 'POST' }); } - -function getCurrentValue(onSuccess, onError) + +function getcounter(onSuccess, onError) { $.ajax( { url: '/counter' - , success: onSuccess + , success: onSuccess , error: onError , type: 'GET' }); } - \ No newline at end of file diff --git a/examples/www/index.html b/examples/www/index.html index 21307b4c..075e6796 100644 --- a/examples/www/index.html +++ b/examples/www/index.html @@ -17,11 +17,11 @@ or view the docs diff --git a/src/Servant/JQuery/Internal.hs b/src/Servant/JQuery/Internal.hs index d419fbf2..ed92c6b7 100644 --- a/src/Servant/JQuery/Internal.hs +++ b/src/Servant/JQuery/Internal.hs @@ -7,6 +7,7 @@ module Servant.JQuery.Internal where import Control.Lens +import Data.Monoid import Data.Proxy import GHC.TypeLits import Servant.API @@ -120,31 +121,31 @@ instance (KnownSymbol sym, HasJQ sublayout) where str = symbolVal (Proxy :: Proxy sym) instance HasJQ Delete where - type JQ Delete = FunctionName -> AjaxReq + type JQ Delete = AjaxReq - jqueryFor Proxy req fName = - req & funcName .~ fName + jqueryFor Proxy req = + req & funcName %~ ("delete" <>) & reqMethod .~ "DELETE" instance HasJQ (Get a) where - type JQ (Get a) = FunctionName -> AjaxReq + type JQ (Get a) = AjaxReq - jqueryFor Proxy req fName = - req & funcName .~ fName + jqueryFor Proxy req = + req & funcName %~ ("get" <>) & reqMethod .~ "GET" instance HasJQ (Post a) where - type JQ (Post a) = FunctionName -> AjaxReq + type JQ (Post a) = AjaxReq - jqueryFor Proxy req fName = - req & funcName .~ fName + jqueryFor Proxy req = + req & funcName %~ ("post" <>) & reqMethod .~ "POST" instance HasJQ (Put a) where - type JQ (Put a) = FunctionName -> AjaxReq + type JQ (Put a) = AjaxReq - jqueryFor Proxy req fName = - req & funcName .~ fName + jqueryFor Proxy req = + req & funcName %~ ("put" <>) & reqMethod .~ "PUT" instance (KnownSymbol sym, HasJQ sublayout) @@ -179,11 +180,10 @@ instance (KnownSymbol sym, HasJQ sublayout) where str = symbolVal (Proxy :: Proxy sym) instance HasJQ Raw where - type JQ Raw = Method -> FunctionName -> AjaxReq + type JQ Raw = Method -> AjaxReq - jqueryFor Proxy req method fName = + jqueryFor Proxy req method = req & reqMethod .~ method - & funcName .~ fName instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where type JQ (ReqBody a :> sublayout) = JQ sublayout @@ -199,5 +199,6 @@ instance (KnownSymbol path, HasJQ sublayout) jqueryFor Proxy req = jqueryFor (Proxy :: Proxy sublayout) $ req & reqUrl.path <>~ [Static str] + & funcName %~ (str <>) where str = symbolVal (Proxy :: Proxy path)