Created
August 30, 2014 19:52
-
-
Save adinapoli/37f359c974cf413ebb1b 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 -> JSString -> 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', processData: false, \ | |
\ contentType: 'text/json', \ | |
\ success: sFn, \ | |
\ error: fFn \ | |
\ })})" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment