first shot at generating function names automatically

This commit is contained in:
Alp Mestanogullari 2014-12-01 17:36:18 +01:00
parent 3e76058d25
commit 99215cd654
4 changed files with 26 additions and 30 deletions

View file

@ -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

View file

@ -1,5 +1,5 @@
function increaseCounter(onSuccess, onError)
function postcounter(onSuccess, onError)
{
$.ajax(
{ url: '/counter'
@ -9,7 +9,7 @@ function increaseCounter(onSuccess, onError)
});
}
function getCurrentValue(onSuccess, onError)
function getcounter(onSuccess, onError)
{
$.ajax(
{ url: '/counter'
@ -18,4 +18,3 @@ function getCurrentValue(onSuccess, onError)
, type: 'GET'
});
}

View file

@ -17,11 +17,11 @@ or <a href="/doc">view the docs</a>
<script type="text/javascript">
$(document).ready(function() {
// we get the current value stored by the server when the page is loaded
getCurrentValue(updateCounter, alert);
getcounter(updateCounter, alert);
// we update the value every 1sec, in the same way
window.setInterval(function() {
getCurrentValue(updateCounter, alert);
getcounter(updateCounter, alert);
}, 1000);
});
@ -33,7 +33,7 @@ function updateCounter(response)
// when the button is clicked, ask the server to increase
// the value by one
$('#inc').click(function() {
increaseCounter(updateCounter, alert);
postcounter(updateCounter, alert);
});
</script>
</body>

View file

@ -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)