Skip to content

Commit 06de068

Browse files
committed
Examples and Makefile: minor fixes
1 parent 9a95904 commit 06de068

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
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/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: 12 additions & 8 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

@@ -19,7 +20,17 @@ module Tutorial where
1920
]
2021

2122
commentBox = mkStatefulUIFromSpec [] cBoxRender $
22-
defaultStatefulSpec { componentWillMount = componentWillMount }
23+
defaultStatefulSpec {
24+
componentWillMount = componentWillMount
25+
}
26+
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+
2334

2435
commentList = mkUI do
2536
props <- getProps
@@ -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)