Created
August 31, 2014 09:40
-
-
Save adinapoli/c75a6c58f13d6e84117d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Haste.JQuery where | |
import Haste.Foreign | |
import Haste.JSON | |
import Haste.Prim | |
import Haste hiding (Event) | |
-------------------------------------------------------------------------------- | |
-- | The JQuery datatype is just a type synonym with Haste's builtin | |
-- "Elem" time, to be backward compatible with "Haste.DOM" | |
type JQuery = Elem | |
-------------------------------------------------------------------------------- | |
type EventType = String | |
-------------------------------------------------------------------------------- | |
ready :: IO () -> IO () | |
ready = ffi "(function (f) { jQuery(document).ready(f); })" | |
-------------------------------------------------------------------------------- | |
select :: ElemID -> IO JQuery | |
select el = let elJs = toJSString el in doFFI elJs | |
where | |
doFFI :: JSString -> IO JQuery | |
doFFI = ffi "(function (elId) { return jQuery(elId); })" | |
-------------------------------------------------------------------------------- | |
on :: EventType -> (JSAny -> IO ()) -> JQuery -> IO () | |
on et fn jQ = let etJs = toJSString et in doFFI etJs fn jQ | |
where | |
doFFI :: JSString -> (JSAny -> IO ()) -> JQuery -> IO () | |
doFFI = ffi "(function (et, fn, domEl) { return domEl.on(et, fn); })" | |
-------------------------------------------------------------------------------- | |
getText :: JQuery -> IO String | |
getText = ffi "(function (domEl) { return domEl.text(); })" | |
-------------------------------------------------------------------------------- | |
attr :: PropID -> String -> JQuery -> IO JQuery | |
attr k v jQ = let kJs = toJSString k; vJs = toJSString v in doFFI kJs vJs jQ | |
where | |
doFFI :: JSString -> JSString -> JQuery -> IO JQuery | |
doFFI = ffi "(function (k,v, domEl) { return domEl.attr(k,v); })" | |
--- | |
--- Ajax Helpers | |
--- | |
-------------------------------------------------------------------------------- | |
type AjaxError = JSAny -> JSString -> JSString -> IO () | |
-------------------------------------------------------------------------------- | |
type AjaxSuccess = JSAny -> IO () | |
-------------------------------------------------------------------------------- | |
ajaxJson :: URL -> JSON -> AjaxSuccess -> AjaxError -> IO () | |
ajaxJson url toSend successFn failureFn = do | |
let jsonAsJs = encodeJSON toSend | |
let urlAsJs = toJSString url | |
doFFI urlAsJs jsonAsJs successFn failureFn | |
where | |
doFFI :: JSString -> JSString -> AjaxSuccess -> AjaxError -> IO () | |
doFFI = ffi "(function(url, data, sFn, fFn) { \ | |
\ jQuery.ajax(url, \ | |
\ { data: JSON.stringify(data), \ | |
\ type: 'POST', cache: false, processData: false, \ | |
\ dataType: 'json', \ | |
\ contentType: 'application/json', \ | |
\ success: function(a,b,c) { return sFn(a); }, \ | |
\ error: fFn \ | |
\ })})" | |
{-- Invocation: | |
ajaxJson "http://echo.jsontest.com/key/value/otherkey/othervalue" | |
mySimpleJSON | |
(\dat -> dumpAny dat >> setButtonState Reset b) | |
(\_ _ _ -> consoleLog "KO") | |
--} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment