first shot at generating function names automatically
This commit is contained in:
parent
3e76058d25
commit
99215cd654
4 changed files with 26 additions and 30 deletions
|
@ -62,10 +62,8 @@ runServer :: TVar Counter -- ^ shared variable for the counter
|
||||||
runServer var port = run port (serve testApi $ server var)
|
runServer var port = run port (serve testApi $ server var)
|
||||||
|
|
||||||
-- * Generating the JQuery code
|
-- * Generating the JQuery code
|
||||||
incCounterNamed :: FunctionName -> AjaxReq
|
|
||||||
currentValueNamed :: FunctionName -> AjaxReq
|
|
||||||
|
|
||||||
incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi
|
incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
|
||||||
|
|
||||||
writeJS :: FilePath -> [AjaxReq] -> IO ()
|
writeJS :: FilePath -> [AjaxReq] -> IO ()
|
||||||
writeJS fp functions = writeFile fp $
|
writeJS fp functions = writeFile fp $
|
||||||
|
@ -75,9 +73,7 @@ main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
-- write the JS code to www/api.js at startup
|
-- write the JS code to www/api.js at startup
|
||||||
writeJS (www </> "api.js")
|
writeJS (www </> "api.js")
|
||||||
[ incCounterNamed "increaseCounter"
|
[ incCounterJS, currentValueJS ]
|
||||||
, currentValueNamed "getCurrentValue"
|
|
||||||
]
|
|
||||||
|
|
||||||
-- setup a shared counter
|
-- setup a shared counter
|
||||||
cnt <- newCounter
|
cnt <- newCounter
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
|
|
||||||
function increaseCounter(onSuccess, onError)
|
function postcounter(onSuccess, onError)
|
||||||
{
|
{
|
||||||
$.ajax(
|
$.ajax(
|
||||||
{ url: '/counter'
|
{ url: '/counter'
|
||||||
, success: onSuccess
|
, success: onSuccess
|
||||||
, error: onError
|
, error: onError
|
||||||
, type: 'POST'
|
, type: 'POST'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentValue(onSuccess, onError)
|
function getcounter(onSuccess, onError)
|
||||||
{
|
{
|
||||||
$.ajax(
|
$.ajax(
|
||||||
{ url: '/counter'
|
{ url: '/counter'
|
||||||
, success: onSuccess
|
, success: onSuccess
|
||||||
, error: onError
|
, error: onError
|
||||||
, type: 'GET'
|
, type: 'GET'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,11 @@ or <a href="/doc">view the docs</a>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
// we get the current value stored by the server when the page is loaded
|
// 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
|
// we update the value every 1sec, in the same way
|
||||||
window.setInterval(function() {
|
window.setInterval(function() {
|
||||||
getCurrentValue(updateCounter, alert);
|
getcounter(updateCounter, alert);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ function updateCounter(response)
|
||||||
// when the button is clicked, ask the server to increase
|
// when the button is clicked, ask the server to increase
|
||||||
// the value by one
|
// the value by one
|
||||||
$('#inc').click(function() {
|
$('#inc').click(function() {
|
||||||
increaseCounter(updateCounter, alert);
|
postcounter(updateCounter, alert);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
module Servant.JQuery.Internal where
|
module Servant.JQuery.Internal where
|
||||||
|
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
|
import Data.Monoid
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
import GHC.TypeLits
|
import GHC.TypeLits
|
||||||
import Servant.API
|
import Servant.API
|
||||||
|
@ -120,31 +121,31 @@ instance (KnownSymbol sym, HasJQ sublayout)
|
||||||
where str = symbolVal (Proxy :: Proxy sym)
|
where str = symbolVal (Proxy :: Proxy sym)
|
||||||
|
|
||||||
instance HasJQ Delete where
|
instance HasJQ Delete where
|
||||||
type JQ Delete = FunctionName -> AjaxReq
|
type JQ Delete = AjaxReq
|
||||||
|
|
||||||
jqueryFor Proxy req fName =
|
jqueryFor Proxy req =
|
||||||
req & funcName .~ fName
|
req & funcName %~ ("delete" <>)
|
||||||
& reqMethod .~ "DELETE"
|
& reqMethod .~ "DELETE"
|
||||||
|
|
||||||
instance HasJQ (Get a) where
|
instance HasJQ (Get a) where
|
||||||
type JQ (Get a) = FunctionName -> AjaxReq
|
type JQ (Get a) = AjaxReq
|
||||||
|
|
||||||
jqueryFor Proxy req fName =
|
jqueryFor Proxy req =
|
||||||
req & funcName .~ fName
|
req & funcName %~ ("get" <>)
|
||||||
& reqMethod .~ "GET"
|
& reqMethod .~ "GET"
|
||||||
|
|
||||||
instance HasJQ (Post a) where
|
instance HasJQ (Post a) where
|
||||||
type JQ (Post a) = FunctionName -> AjaxReq
|
type JQ (Post a) = AjaxReq
|
||||||
|
|
||||||
jqueryFor Proxy req fName =
|
jqueryFor Proxy req =
|
||||||
req & funcName .~ fName
|
req & funcName %~ ("post" <>)
|
||||||
& reqMethod .~ "POST"
|
& reqMethod .~ "POST"
|
||||||
|
|
||||||
instance HasJQ (Put a) where
|
instance HasJQ (Put a) where
|
||||||
type JQ (Put a) = FunctionName -> AjaxReq
|
type JQ (Put a) = AjaxReq
|
||||||
|
|
||||||
jqueryFor Proxy req fName =
|
jqueryFor Proxy req =
|
||||||
req & funcName .~ fName
|
req & funcName %~ ("put" <>)
|
||||||
& reqMethod .~ "PUT"
|
& reqMethod .~ "PUT"
|
||||||
|
|
||||||
instance (KnownSymbol sym, HasJQ sublayout)
|
instance (KnownSymbol sym, HasJQ sublayout)
|
||||||
|
@ -179,11 +180,10 @@ instance (KnownSymbol sym, HasJQ sublayout)
|
||||||
where str = symbolVal (Proxy :: Proxy sym)
|
where str = symbolVal (Proxy :: Proxy sym)
|
||||||
|
|
||||||
instance HasJQ Raw where
|
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
|
req & reqMethod .~ method
|
||||||
& funcName .~ fName
|
|
||||||
|
|
||||||
instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
|
instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
|
||||||
type JQ (ReqBody a :> sublayout) = JQ sublayout
|
type JQ (ReqBody a :> sublayout) = JQ sublayout
|
||||||
|
@ -199,5 +199,6 @@ instance (KnownSymbol path, HasJQ sublayout)
|
||||||
jqueryFor Proxy req =
|
jqueryFor Proxy req =
|
||||||
jqueryFor (Proxy :: Proxy sublayout) $
|
jqueryFor (Proxy :: Proxy sublayout) $
|
||||||
req & reqUrl.path <>~ [Static str]
|
req & reqUrl.path <>~ [Static str]
|
||||||
|
& funcName %~ (str <>)
|
||||||
|
|
||||||
where str = symbolVal (Proxy :: Proxy path)
|
where str = symbolVal (Proxy :: Proxy path)
|
||||||
|
|
Loading…
Reference in a new issue