Skip to content

Commit 9dd8258

Browse files
committed
reimplemented AsyncProps
now loads all async props in parallel instead of the waterfall from before
1 parent e60cda7 commit 9dd8258

File tree

11 files changed

+349
-136
lines changed

11 files changed

+349
-136
lines changed

examples/async-data/app.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ var Spinner = React.createClass({
1717
var App = React.createClass({
1818
mixins: [ Navigation ],
1919

20+
getDefaultProps() {
21+
return { contacts: [] };
22+
},
23+
2024
statics: {
2125
loadProps(params, cb) {
2226
loadContacts(cb);
23-
},
24-
25-
Loader: Spinner
27+
}
2628
},
2729

2830
handleSubmit(event) {
@@ -31,22 +33,28 @@ var App = React.createClass({
3133
first: event.target.elements[0].value,
3234
last: event.target.elements[1].value
3335
}, (err, data) => {
34-
this.props.onPropsDidChange();
36+
this.props.reloadAsyncProps();
3537
this.transitionTo(`/contact/${data.contact.id}`);
3638
});
3739
event.target.reset();
3840
event.target.elements[0].focus();
3941
},
4042

4143
render() {
44+
// super smooth user feedback
45+
var appStyle = {
46+
transition: this.props.loading ? 'opacity 500ms ease 250ms' : 'opacity 150ms',
47+
opacity: this.props.loading ? 0.5 : 1
48+
};
49+
4250
return (
43-
<div className="App">
51+
<div className="App" style={appStyle}>
4452
<form onSubmit={this.handleSubmit}>
4553
<input placeholder="First name"/> <input placeholder="Last name"/>{' '}
4654
<button type="submit">submit</button>
4755
</form>
4856
<div style={{display: 'flex'}}>
49-
<ul style={{opacity: this.props.propsAreLoadingLong ? 0.5 : 1, padding: 20}}>
57+
<ul style={{opacity: this.props.loadingAsyncProps ? 0.5 : 1, padding: 20}}>
5058
{this.props.contacts.map((contact, i) => (
5159
<li key={contact.id}>
5260
<Link to={`/contact/${contact.id}`}>{contact.first} {contact.last}</Link>
@@ -64,29 +72,24 @@ var App = React.createClass({
6472

6573
var Contact = React.createClass({
6674

75+
getDefaultProps() {
76+
return { contact: {} };
77+
},
78+
6779
statics: {
6880
loadProps(params, cb) {
69-
//console.log('Contact loadProps');
7081
loadContact(params.id, cb);
71-
},
72-
73-
Loader: Spinner
74-
},
75-
76-
getInitialState() {
77-
return {
78-
longLoad: false
79-
};
82+
}
8083
},
8184

8285
render() {
8386
var { contact } = this.props;
8487

8588
return (
86-
<div style={{opacity: this.props.propsAreLoadingLong ? 0.5 : 1}}>
89+
<div style={{opacity: this.props.loadingAsyncProps ? 0.5 : 1}}>
8790
<p><Link to="/">Back</Link></p>
8891
<h1>{contact.first} {contact.last}</h1>
89-
<img key={contact.avatar} src={contact.avatar} height="200"/>
92+
<p><img key={contact.avatar} src={contact.avatar} height="200"/></p>
9093
</div>
9194
);
9295
}
@@ -103,10 +106,12 @@ var Index = React.createClass({
103106
});
104107

105108
React.render((
106-
<Router history={new HashHistory} createElement={AsyncProps.createElement}>
107-
<Route component={App}>
108-
<Route path="/" component={Index}/>
109-
<Route path="contact/:id" component={Contact}/>
109+
<Router history={new HashHistory()} createElement={AsyncProps.createElement}>
110+
<Route component={AsyncProps} renderInitialLoad={() => <Spinner/> }>
111+
<Route component={App}>
112+
<Route path="/" component={Index}/>
113+
<Route path="contact/:id" component={Contact}/>
114+
</Route>
110115
</Route>
111116
</Router>
112117
), document.getElementById('example'));

examples/async-data/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var API = 'http://addressbook-api.herokuapp.com';
1+
//var API = 'http://addressbook-api.herokuapp.com';
2+
var API = 'http://localhost:3000';
23

34
localStorage.token = localStorage.token || (Date.now()*Math.random());
45

@@ -32,6 +33,7 @@ function getJSON(url, cb) {
3233
cb(new Error('not found'));
3334
} else {
3435

36+
var time = 0;
3537
// fake a spotty server
3638
var time = Math.random() * 1000;
3739
// for a really spotty server:

examples/huge-apps/app.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@ import AsyncProps from 'react-router/lib/experimental/AsyncProps';
55
import stubbedCourses from './stubs/COURSES';
66

77
var rootRoute = {
8-
path: '/',
8+
component: AsyncProps,
99

10-
childRoutes: [
11-
require('./routes/Calendar'),
12-
require('./routes/Course'),
13-
require('./routes/Grades'),
14-
require('./routes/Messages'),
15-
require('./routes/Profile'),
16-
],
10+
// iunno?
11+
renderInitialLoad() {
12+
return <div>loading...</div>
13+
},
1714

18-
component: require('./components/App'),
15+
childRoutes: [{
16+
path: '/',
17+
component: require('./components/App'),
18+
childRoutes: [
19+
require('./routes/Calendar'),
20+
require('./routes/Course'),
21+
require('./routes/Grades'),
22+
require('./routes/Messages'),
23+
require('./routes/Profile'),
24+
]}
25+
]
1926
};
2027

2128
React.render((
2229
<Router
23-
children={rootRoute}
24-
history={new HashHistory}
30+
routes={rootRoute}
31+
history={new HashHistory()}
2532
createElement={AsyncProps.createElement}
2633
/>
2734
), document.getElementById('example'));

examples/huge-apps/components/App.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import GlobalNav from './GlobalNav';
44

55
class App extends React.Component {
66

7-
//static loadProps (params, cb) {
8-
//console.log('App');
9-
//cb(null, { courses: COURSES });
10-
//}
7+
static loadProps (params, cb) {
8+
console.log('App', 'loadProps');
9+
cb(null, { courses: COURSES });
10+
}
1111

1212
render () {
13-
//var { courses } = this.props;
14-
var courses = COURSES;
13+
var { courses, loading } = this.props;
1514
return (
16-
<div>
15+
<div style={{opacity: loading ? 0.66 : 1}}>
1716
<GlobalNav/>
1817
<div style={{padding: 20}}>
1918
{this.props.children || <Dashboard courses={courses}/>}

examples/huge-apps/components/GlobalNav.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class GlobalNav extends React.Component {
2727
<div style={styles.wrapper}>
2828
<div style={{float: 'left'}}>
2929
<Link to="/" style={styles.link}>Home</Link>{' '}
30-
<Link to="/calendar" style={styles.link}>Calendar</Link>{' '}
31-
<Link to="/grades" style={styles.link}>Grades</Link>{' '}
32-
<Link to="/messages" style={styles.link}>Messages</Link>{' '}
30+
<Link to="/calendar" style={styles.link} activeStyle={styles.activeLink}>Calendar</Link>{' '}
31+
<Link to="/grades" style={styles.link} activeStyle={styles.activeLink}>Grades</Link>{' '}
32+
<Link to="/messages" style={styles.link} activeStyle={styles.activeLink}>Messages</Link>{' '}
3333
</div>
3434
<div style={{float: 'right'}}>
3535
<Link style={styles.link} to="/profile">{user.name}</Link> <button onClick={this.logOut}>log out</button>
@@ -39,18 +39,28 @@ class GlobalNav extends React.Component {
3939
}
4040
}
4141

42+
var dark = 'hsl(200, 20%, 20%)';
43+
var light = '#fff';
44+
4245
styles.wrapper = {
4346
padding: '10px 20px',
4447
overflow: 'hidden',
45-
background: 'hsl(200, 20%, 20%)',
46-
color: '#fff'
48+
background: dark,
49+
color: light
4750
};
4851

4952
styles.link = {
50-
padding: 10,
51-
color: '#fff',
53+
padding: 11,
54+
color: light,
5255
fontWeight: 200
5356
}
5457

58+
styles.activeLink = Object.assign({}, styles.link, {
59+
background: light,
60+
color: dark
61+
});
62+
63+
console.log(styles.link);
64+
5565
export default GlobalNav;
5666

examples/huge-apps/routes/Calendar/components/Calendar.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@ import React from 'react';
22

33
class Calendar extends React.Component {
44

5+
static loadProps(params, cb) {
6+
setTimeout(() => {
7+
cb(null, {
8+
events: [{
9+
id: 0, title: 'essay due'
10+
}]
11+
})
12+
}, 1000);
13+
}
14+
515
render () {
16+
var { events } = this.props;
617
return (
718
<div>
819
<h2>Calendar</h2>
20+
<ul>
21+
{events.map(event => (
22+
<li key={event.id}>{event.title}</li>
23+
))}
24+
</ul>
925
</div>
1026
);
1127
}

examples/huge-apps/routes/Course/components/Course.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ styles.sidebar = {
1414

1515
class Course extends React.Component {
1616

17-
//static loadProps (params, cb) {
18-
//cb(null, { course: COURSES[params.courseId] });
19-
//}
17+
static loadProps (params, cb) {
18+
console.log('Course', 'loadProps');
19+
setTimeout(() => cb(null, { course: COURSES[params.courseId] }), 1000);
20+
}
2021

2122
render () {
22-
//var { course } = this.props;
23-
var course = COURSES[this.props.params.courseId];
23+
var { course } = this.props;
2424
return (
2525
<div>
2626
<h2>{course.name}</h2>

examples/huge-apps/routes/Course/routes/Announcements/components/Sidebar.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { Link } from 'react-router';
33

44
export default class AnnouncementsSidebar extends React.Component {
55

6-
//static loadProps (params, cb) {
7-
//cb(null, {
8-
//announcements: COURSES[params.courseId].announcements
9-
//});
10-
//}
6+
static loadProps (params, cb) {
7+
console.log('AnnouncementsSidebar', 'loadProps');
8+
cb(null, {
9+
announcements: COURSES[params.courseId].announcements
10+
});
11+
}
1112

1213
render () {
13-
//var { announcements } = this.props;
14-
var announcements = COURSES[this.props.params.courseId].announcements;
14+
var { announcements } = this.props;
15+
//var announcements = COURSES[this.props.params.courseId].announcements;
1516
return (
1617
<div>
1718
<h3>Sidebar Assignments</h3>

modules/Router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ var Router = React.createClass({
262262
},
263263

264264
render() {
265-
var { location, branch, params, components, isTransitioning } = this.state;
265+
var { branch, params, components } = this.state;
266266
var element = null;
267267

268268
if (components) {
@@ -272,7 +272,7 @@ var Router = React.createClass({
272272

273273
var route = branch[index];
274274
var routeParams = getRouteParams(route, params);
275-
var props = { location, params, route, routeParams, isTransitioning };
275+
var props = Object.assign({}, this.state, { route, routeParams });
276276

277277
if (isValidElement(element)) {
278278
props.children = element;

0 commit comments

Comments
 (0)