git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Improved jQuery library and example
commit8d52e8d75d11eaded8284799362cbd45f4c82167 authorPhil Freeman <paf31@cantab.net> dateSun 12 Jan 2014, 18:50 (13 years ago) parent76ee160
5 files changed+47−2
libraries/jquery/jquery.purs+15 libraries/jquery/tests/.gitignore+1 libraries/jquery/tests/build.sh+2 libraries/jquery/tests/index.html+10 libraries/jquery/tests/jquery.purs+19−2
libraries/jquery/jquery.purs+15
@@ -28,3 +28,18 @@ module JQuery where
2828
2929 -- Get the document body
3030 foreign import body "function body() { return jQuery(document.body); }" :: forall eff. Eff (dom :: DOM | eff) JQuery
31+
32+ -- Get the text content of an element
33+ foreign import getText "function getText(ob) { return function() { return ob.text(); }; }" :: forall eff. JQuery -> Eff (dom :: DOM | eff) String
34+
35+ -- Set the text content of an element
36+ foreign import setText "function setText(text) { return function(ob) { return function() { ob.text(text); }; }; }" :: forall eff. String -> JQuery -> Eff (dom :: DOM | eff) JQuery
37+
38+ -- Get the value of a text field
39+ foreign import getValue "function getValue(ob) { return function() { return ob.val(); }; }" :: forall eff. JQuery -> Eff (dom :: DOM | eff) String
40+
41+ -- Set the value of a text field
42+ foreign import setValue "function setValue(val) { return function(ob) { return function() { return ob.val(val); }; }; }" :: forall eff. String -> JQuery -> Eff (dom :: DOM | eff) JQuery
43+
44+ -- Register an event handler
45+ foreign import on "function on(evt) { return function(act) { return function(ob) { return function() { return ob.on(evt, function() { act(); } ); }; }; }; }" :: forall eff a. String -> Eff eff a -> JQuery -> Eff (dom :: DOM | eff) JQuery
libraries/jquery/tests/.gitignorenew file+1
@@ -0,0 +1 @@
1+jquery.js
libraries/jquery/tests/build.shnew file+2
@@ -0,0 +1,2 @@
1+psc ../jquery.purs jquery.purs --magic-do --run-main -o jquery.js
2+
libraries/jquery/tests/index.htmlnew file+10
@@ -0,0 +1,10 @@
1+<!DOCTYPE html>
2+<html>
3+<head>
4+<title>JQuery Example</title>
5+<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
6+</head>
7+<body>
8+<script src="jquery.js"></script>
9+</body>
10+</html>
libraries/jquery/tests/jquery.purs+19−2
@@ -1,11 +1,28 @@
11module Main where
22
3+import Prelude
34import Eff
5+import Trace
46import JQuery
57
68main = eff do
9+ -- Get the document body
710 b <- body
11+
12+ -- Create a text box
813 div <- create "<div>"
9− { color: "red" } `css` div
10− appendText "Hello World" div
14+ input <- create "<input>"
15+ "Your Name: " `appendText` div
16+ input `append` div
1117 div `append` b
18+
19+ -- Create a paragraph to display a greeting
20+ greeting <- create "<p>"
21+ { color: "red" } `css` greeting
22+ greeting `append` b
23+
24+ -- Listen for change events on the text box
25+ flip (on "change") input $ eff do
26+ name <- getValue input
27+ trace $ "Name changed to " ++ name
28+ setText ("Hello, " ++ name) greeting