on master help sign in
first shot at generating function names automatically
99215cdAlp Mestanogullari
examples/counter.hs+2−6
@@ -62,10 +62,8 @@ runServer :: TVar Counter -- ^ shared variable for the counter
62runServer var port = run port (serve testApi $ server var)
62runServer var port = run port (serve testApi $ server var)
63
63
64-- * Generating the JQuery code
64-- * Generating the JQuery code
65−incCounterNamed :: FunctionName -> AjaxReq
66−currentValueNamed :: FunctionName -> AjaxReq
67
65
68−incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi
66+incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
69
67
70writeJS :: FilePath -> [AjaxReq] -> IO ()
68writeJS :: FilePath -> [AjaxReq] -> IO ()
71writeJS fp functions = writeFile fp $
69writeJS fp functions = writeFile fp $
@@ -75,9 +73,7 @@ main :: IO ()
75main = do
73main = do
76 -- write the JS code to www/api.js at startup
74 -- write the JS code to www/api.js at startup
77 writeJS (www </> "api.js")
75 writeJS (www </> "api.js")
78− [ incCounterNamed "increaseCounter"
76+ [ incCounterJS, currentValueJS ]
79− , currentValueNamed "getCurrentValue"
80− ]
81
77
82 -- setup a shared counter
78 -- setup a shared counter
83 cnt <- newCounter
79 cnt <- newCounter
examples/www/api.js+5−6
@@ -1,21 +1,20 @@
1
1
2−function increaseCounter(onSuccess, onError)
2+function postcounter(onSuccess, onError)
3{
3{
4 $.ajax(
4 $.ajax(
5 { url: '/counter'
5 { url: '/counter'
6− , success: onSuccess
6+ , success: onSuccess
7 , error: onError
7 , error: onError
8 , type: 'POST'
8 , type: 'POST'
9 });
9 });
10}
10}
11−
11+
12−function getCurrentValue(onSuccess, onError)
12+function getcounter(onSuccess, onError)
13{
13{
14 $.ajax(
14 $.ajax(
15 { url: '/counter'
15 { url: '/counter'
16− , success: onSuccess
16+ , success: onSuccess
17 , error: onError
17 , error: onError
18 , type: 'GET'
18 , type: 'GET'
19 });
19 });
20}
20}
21−
examples/www/index.html+3−3
@@ -17,11 +17,11 @@ or <a href="/doc">view the docs</a>
17<script type="text/javascript">
17<script type="text/javascript">
18$(document).ready(function() {
18$(document).ready(function() {
19 // we get the current value stored by the server when the page is loaded
19 // we get the current value stored by the server when the page is loaded
20− getCurrentValue(updateCounter, alert);
20+ getcounter(updateCounter, alert);
21
21
22 // we update the value every 1sec, in the same way
22 // we update the value every 1sec, in the same way
23 window.setInterval(function() {
23 window.setInterval(function() {
24− getCurrentValue(updateCounter, alert);
24+ getcounter(updateCounter, alert);
25 }, 1000);
25 }, 1000);
26});
26});
27
27
@@ -33,7 +33,7 @@ function updateCounter(response)
33// when the button is clicked, ask the server to increase
33// when the button is clicked, ask the server to increase
34// the value by one
34// the value by one
35$('#inc').click(function() {
35$('#inc').click(function() {
36− increaseCounter(updateCounter, alert);
36+ postcounter(updateCounter, alert);
37});
37});
38</script>
38</script>
39</body>
39</body>
src/Servant/JQuery/Internal.hs+16−15
@@ -7,6 +7,7 @@
7module Servant.JQuery.Internal where
7module Servant.JQuery.Internal where
8
8
9import Control.Lens
9import Control.Lens
10+import Data.Monoid
10import Data.Proxy
11import Data.Proxy
11import GHC.TypeLits
12import GHC.TypeLits
12import Servant.API
13import Servant.API
@@ -120,31 +121,31 @@ instance (KnownSymbol sym, HasJQ sublayout)
120 where str = symbolVal (Proxy :: Proxy sym)
121 where str = symbolVal (Proxy :: Proxy sym)
121
122
122instance HasJQ Delete where
123instance HasJQ Delete where
123− type JQ Delete = FunctionName -> AjaxReq
124+ type JQ Delete = AjaxReq
124
125
125− jqueryFor Proxy req fName =
126+ jqueryFor Proxy req =
126− req & funcName .~ fName
127+ req & funcName %~ ("delete" <>)
127 & reqMethod .~ "DELETE"
128 & reqMethod .~ "DELETE"
128
129
129instance HasJQ (Get a) where
130instance HasJQ (Get a) where
130− type JQ (Get a) = FunctionName -> AjaxReq
131+ type JQ (Get a) = AjaxReq
131
132
132− jqueryFor Proxy req fName =
133+ jqueryFor Proxy req =
133− req & funcName .~ fName
134+ req & funcName %~ ("get" <>)
134 & reqMethod .~ "GET"
135 & reqMethod .~ "GET"
135
136
136instance HasJQ (Post a) where
137instance HasJQ (Post a) where
137− type JQ (Post a) = FunctionName -> AjaxReq
138+ type JQ (Post a) = AjaxReq
138
139
139− jqueryFor Proxy req fName =
140+ jqueryFor Proxy req =
140− req & funcName .~ fName
141+ req & funcName %~ ("post" <>)
141 & reqMethod .~ "POST"
142 & reqMethod .~ "POST"
142
143
143instance HasJQ (Put a) where
144instance HasJQ (Put a) where
144− type JQ (Put a) = FunctionName -> AjaxReq
145+ type JQ (Put a) = AjaxReq
145
146
146− jqueryFor Proxy req fName =
147+ jqueryFor Proxy req =
147− req & funcName .~ fName
148+ req & funcName %~ ("put" <>)
148 & reqMethod .~ "PUT"
149 & reqMethod .~ "PUT"
149
150
150instance (KnownSymbol sym, HasJQ sublayout)
151instance (KnownSymbol sym, HasJQ sublayout)
@@ -179,11 +180,10 @@ instance (KnownSymbol sym, HasJQ sublayout)
179 where str = symbolVal (Proxy :: Proxy sym)
180 where str = symbolVal (Proxy :: Proxy sym)
180
181
181instance HasJQ Raw where
182instance HasJQ Raw where
182− type JQ Raw = Method -> FunctionName -> AjaxReq
183+ type JQ Raw = Method -> AjaxReq
183
184
184− jqueryFor Proxy req method fName =
185+ jqueryFor Proxy req method =
185 req & reqMethod .~ method
186 req & reqMethod .~ method
186− & funcName .~ fName
187
187
188instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
188instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
189 type JQ (ReqBody a :> sublayout) = JQ sublayout
189 type JQ (ReqBody a :> sublayout) = JQ sublayout
@@ -199,5 +199,6 @@ instance (KnownSymbol path, HasJQ sublayout)
199 jqueryFor Proxy req =
199 jqueryFor Proxy req =
200 jqueryFor (Proxy :: Proxy sublayout) $
200 jqueryFor (Proxy :: Proxy sublayout) $
201 req & reqUrl.path <>~ [Static str]
201 req & reqUrl.path <>~ [Static str]
202+ & funcName %~ (str <>)
202
203
203 where str = symbolVal (Proxy :: Proxy path)
204 where str = symbolVal (Proxy :: Proxy path)