Permalink
Browse files

* Comments

* Small additions
  • Loading branch information...
1 parent 9b6a3e9 commit a3d3e2f38e8ee55a8a4e55bc81e2403deb00d5dc @spockz spockz committed Jan 25, 2012
@@ -10,14 +10,16 @@ import Data.List
data JQXHRPtr
type JQXHR = JSPtr JQXHRPtr
-
+-- These two types themselves do not contain the constraint JS r as these types
+-- are also used in the wrapper functions. The FFI does not support classes so
+-- hence their absence here.
type AjaxCallback r = r -> String -> JQXHR -> IO()
type JSAjaxCallback r = JSFunPtr (AjaxCallback r)
-data AjaxRequestType = GET | POST | DELETE
+data AjaxRequestType = GET | HEAD | POST | PUT | DELETE
deriving Show
-
+-- Records for convenience passing
data AjaxOptions a = AjaxOptions {
ao_url :: String,
ao_requestType :: AjaxRequestType,
@@ -39,6 +41,8 @@ instance Show (AjaxOptions a) where
instance Show (JSAjaxOptions a) where
show jsopt = "JSAjaxOptions: " ++ intercalate " " [show $ url jsopt]
+-- | It should be possible to do this automatically by using generics with
+-- Generic Deriving
toJSOptions :: AjaxOptions a -> JSAjaxOptions a
toJSOptions options = let url' = toJS (ao_url options)
requestType' = toJS (show $ ao_requestType options)
@@ -50,7 +54,9 @@ toJSOptions options = let url' = toJS (ao_url options)
, dataType = dataType'
}
-
+-- | Wrapper function that processes the needed arguments before passing it
+-- to |cont| that is responsible for doing the request. One can also partially
+-- apply this to get insert a debugger for requests.
ajaxBackend :: (JS r, JS v) => (JSPtr a -> IO ()) -> AjaxOptions a -> v -> AjaxCallback r -> AjaxCallback r -> IO ()
ajaxBackend cont options valdata onSuccess onFailure =
do let jsOptions = toJSOptions options
@@ -61,8 +67,9 @@ ajaxBackend cont options valdata onSuccess onFailure =
_ <- setAttr "success" onSuccess' o
_ <- setAttr "error" onFailure' o
_ <- setAttr "data" valdata o
- _ajaxQ (toJS "jcu_app") o
+ cont o
+-- | Using the standard jQuery ajax function for executing the jQuery funcitons.
ajax :: (JS r, JS v) => AjaxOptions a -> v -> AjaxCallback r -> AjaxCallback r -> IO ()
ajax = ajaxBackend _ajax
@@ -74,8 +81,5 @@ foreign import jscript "wrapper"
foreign import jscript "$.ajax(%1)"
_ajax :: JSPtr a -> IO ()
-foreign import jscript "$.ajaxq(%*)"
- _ajaxQ :: JSString -> JSPtr a -> IO ()
-
noop :: AjaxCallback a
noop _ _ _ = return ()
@@ -1,3 +1,5 @@
+-- | Binding for the jquery-ajaxq library by Oleg Podolsky.
+-- It can be found at: http://code.google.com/p/jquery-ajaxq/
module Language.UHC.JScript.JQuery.AjaxQueue (ajaxQ) where
import Language.UHC.JScript.Primitives
@@ -8,6 +10,7 @@ import Language.UHC.JScript.JQuery.Ajax
import Language.UHC.JScript.Assorted (alert, _alert)
+-- | Partial application of the backend for use with the AjaxQueue library
ajaxQ :: (JS r, JS v) => String -> AjaxOptions a -> v -> AjaxCallback r -> AjaxCallback r -> IO ()
ajaxQ queuename = ajaxBackend (_ajaxQ $ toJS queuename)
@@ -9,7 +9,7 @@ import Language.UHC.JScript.Assorted (alert)
data JQueryPtr
type JQuery = JSPtr JQueryPtr
-
+type Selector = String
-------------------------------------------------------------------------------
-- jQuery Core
@@ -243,6 +243,11 @@ bind jq event eh = do handler <- mkJEventHandler eh
foreign import jscript "%1.bind(%*)"
_bind :: JQuery -> JSString -> JEventHandler -> IO ()
+registerEvents :: [(String, JEventType, EventHandler)] -> IO ()
+registerEvents = mapM_ (\ (e, event, eh) -> do elem <- jQuery e
+ unbind elem event
+ bind elem event eh)
+
unbind :: JQuery -> JEventType -> IO ()
unbind jq = _unbind jq . toJS . show

0 comments on commit a3d3e2f

Please sign in to comment.