Skip to content

Commit e5d5e2d

Browse files
committed
Merge pull request #9 from andreypopp/next
Next
2 parents 26cb2ce + 2133f7c commit e5d5e2d

File tree

8 files changed

+390
-403
lines changed

8 files changed

+390
-403
lines changed

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,32 @@ PSC_OPTS =
44
all: lib example
55

66
lib:: lib/React.js
7-
example:: example/app.js example/bower_components
7+
examples:: example/app example/tutorial
88

9-
example/bower_components:
10-
(cd example && bower install react)
9+
example/app:: example/app/app.js example/app/bower_components
10+
example/tutorial:: example/tutorial/tutorial.js example/tutorial/bower_components
11+
12+
example/app/bower_components:
13+
(cd $(@D) && bower install react)
14+
15+
example/tutorial/bower_components:
16+
(cd $(@D) && bower install)
1117

1218
clean:
13-
rm -f lib/React.js example/app.js
14-
rm -rf example/bower_components/
19+
rm -f lib/React.js example/app/app.js example/tutorial/tutorial.js
1520

1621
lib/React.js: src/React.purs src/React/DOM.purs
1722
mkdir -p $(@D)
1823
psc $(PSC_OPTS) $^ \
1924
--output $@ \
2025
--module React --module React.DOM
2126

22-
example/app.js: src/React.purs src/React/DOM.purs example/app.purs
27+
example/app/app.js: src/React.purs src/React/DOM.purs example/app/app.purs
2328
psc $(PSC_OPTS) $^ \
2429
--output $@ \
2530
--main --module Main
31+
32+
example/tutorial/tutorial.js: src/React.purs src/React/DOM.purs example/tutorial/tutorial.purs
33+
psc $(PSC_OPTS) $^ $(shell find $(@D)/bower_components -name '*.purs') \
34+
--output $@ \
35+
--module Tutorial --main Tutorial

example/app/app.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ helloInConsole = do
88
props <- getProps
99
trace ("Hello, " ++ props.name ++ "!")
1010

11-
hello = mkUI do
11+
hello = mkUI spec do
1212
props <- getProps
1313
return $ DOM.h1 {
1414
className: "Hello",
@@ -22,7 +22,7 @@ incrementCounter = do
2222
val <- readState
2323
writeState (val + 1)
2424

25-
counter = mkStatefulUI 0 do
25+
counter = mkUI spec { getInitialState = return 0 } do
2626
val <- readState
2727
return $ DOM.p {
2828
className: "Counter",

example/app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</style>
1212
</head>
1313
<body>
14-
<script src="../../bower_components/react/react.js"></script>
14+
<script src="bower_components/react/react.js"></script>
1515
<script src="app.js"></script>
1616
</body>
1717
</html>

example/tutorial/comments.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
[{
2-
"text": "This is one comment",
3-
"author": "Pete Hunt"
4-
}, {
5-
"text": "This is *another* comment",
6-
"author": "Jordan Walke"
7-
}]
1+
[{"text": "This is one comment", "author": "Pete Hunt"}, {"text": "This is *another* comment", "author": "Jordan Walke"}, {"text": "React + PureScript = Love", "author": "Andrey Popp"}]

example/tutorial/server.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,20 @@ class ReactTutorialHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler)
1616
USAGE: python server.py to serve files from the cwd
1717
(works the same as running python -m SimpleHTTPServer in the directory)
1818
"""
19+
1920
def do_POST(self):
2021
# (1) get posted data & convert it to python dict
2122
content_length = int(self.headers['Content-Length'])
2223
post_data = dict(urlparse.parse_qsl(self.rfile.read(content_length).decode('utf-8')))
2324
# (2) open the file at the requested URL (404 if bad)
24-
with open(self.translate_path(self.path), 'rw+') as f:
25-
data = f.read()
26-
# (3) load the file's contents into a list & append the posted data
27-
current_list = json.loads(data)
28-
current_list.append(post_data)
29-
# (4) write the updated content back to the file
30-
json_data = json.dumps(current_list)
31-
f.seek(0)
32-
f.write(json_data)
25+
with open(self.translate_path(self.path), 'r+w') as f:
26+
comments = json.loads(f.read())
27+
comments.append(post_data)
28+
f.seek(0, 0)
29+
f.write(json.dumps(comments))
3330
f.truncate()
34-
# (5) return the updated content (defers to do_GET)
35-
return self.do_GET()
3631

32+
return self.do_GET()
3733

3834
def test(HandlerClass=ReactTutorialHTTPRequestHandler,
3935
ServerClass=BaseHTTPServer.HTTPServer):

example/tutorial/tutorial.purs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Tutorial where
77
import Data.Array
88
import React
99
import Showdown
10+
import Debug.Trace
1011

1112
import qualified React.DOM as DOM
1213

@@ -18,15 +19,25 @@ module Tutorial where
1819
, commentForm { onCommentSubmit: handle commentSubmit }
1920
]
2021

21-
commentBox = mkStatefulUIFromSpec [] cBoxRender $
22-
defaultStatefulSpec { componentWillMount = componentWillMount }
22+
commentBox = mkUI spec {
23+
getInitialState = return [],
24+
componentWillMount = componentWillMount
25+
} cBoxRender
2326

24-
commentList = mkUI do
27+
foreign import componentWillMount
28+
"function componentWillMount() {\
29+
\ var load = loadCommentsFromServer.bind(this);\
30+
\ load();\
31+
\ setInterval(function() { load(); }, this.props.pollInterval);\
32+
\}" :: forall eff props state. ReadState eff props state {}
33+
34+
35+
commentList = mkUI spec do
2536
props <- getProps
2637
pure $ DOM.div { className: "commentList" }
2738
(commentNodes <$> props.data')
2839

29-
commentForm = mkUI do
40+
commentForm = mkUI spec do
3041
props <- getProps
3142
pure $ DOM.form { className: "commentForm"
3243
, onSubmit: handle submit
@@ -47,7 +58,7 @@ module Tutorial where
4758
[]
4859
]
4960

50-
comment = mkUI do
61+
comment = mkUI spec do
5162
props <- getProps
5263
pure $ DOM.div { className: "comment" }
5364
[ DOM.h2 { className: "commentAuthor" }
@@ -95,13 +106,6 @@ module Tutorial where
95106
\ });\
96107
\}" :: forall a r. {props :: {url :: String}, replaceState :: {state :: a} -> {} | r} -> {}
97108

98-
foreign import componentWillMount
99-
"function componentWillMount() {\
100-
\ var load = loadCommentsFromServer.bind(this);\
101-
\ load();\
102-
\ setInterval(function() { load(); }, this.props.pollInterval);\
103-
\}" :: forall r. {} -> {}
104-
105109
main = renderToElementById "content" $ commentBox { url: "comments.json"
106110
, pollInterval: 2000
107111
}

0 commit comments

Comments
 (0)