Skip to content

Commit 4f9ac31

Browse files
committed
demo updates
- updated data flow example to show how to pass props/handlers down the route view hierarchy - fixed simple-master-detail
1 parent 3b590e0 commit 4f9ac31

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

examples/data-flow/app.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,30 @@ var Router = require('../../modules/main');
44
var Route = Router.Route;
55
var Link = Router.Link;
66

7-
// Taco store!
8-
9-
var _tacos = [
10-
{ name: 'duck confit' },
11-
{ name: 'carne asada' },
12-
{ name: 'shrimp' }
13-
];
14-
15-
var tacoStore = {
16-
getAll: function () {
17-
return _tacos;
18-
},
19-
addTaco: function (taco) {
20-
_tacos.push(taco);
21-
this.onChange();
22-
},
23-
onChange: function () {}
24-
};
25-
26-
// Components
27-
287
var App = React.createClass({
298
getInitialState: function() {
309
return {
31-
tacos: tacoStore.getAll()
10+
tacos: [
11+
{ name: 'duck confit' },
12+
{ name: 'carne asada' },
13+
{ name: 'shrimp' }
14+
]
3215
};
3316
},
3417

3518
addTaco: function() {
3619
var name = prompt('taco name?');
37-
tacoStore.addTaco({ name: name });
38-
},
39-
40-
componentWillMount: function () {
41-
tacoStore.onChange = this.updateTacos;
20+
this.setState({
21+
tacos: this.state.tacos.concat({name: name})
22+
});
4223
},
4324

44-
updateTacos: function () {
45-
this.setState({ tacos: tacoStore.getAll() });
25+
handleRemoveTaco: function(removedTaco) {
26+
var tacos = this.state.tacos.filter(function(taco) {
27+
return taco.name != removedTaco;
28+
});
29+
this.setState({tacos: tacos});
30+
Router.transitionTo('/');
4631
},
4732

4833
render: function() {
@@ -56,18 +41,23 @@ var App = React.createClass({
5641
{links}
5742
</ul>
5843
<div className="Detail">
59-
{this.props.activeRoute()}
44+
{this.props.activeRoute({onRemoveTaco: this.handleRemoveTaco})}
6045
</div>
6146
</div>
6247
);
6348
}
6449
});
6550

6651
var Taco = React.createClass({
52+
remove: function() {
53+
this.props.onRemoveTaco(this.props.params.name);
54+
},
55+
6756
render: function() {
6857
return (
6958
<div className="Taco">
7059
<h1>{this.props.params.name}</h1>
60+
<button onClick={this.remove}>remove</button>
7161
</div>
7262
);
7363
}

examples/simple-master-detail/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var App = React.createClass({
2323
{links}
2424
</ul>
2525
<div className="Detail">
26-
{this.props.activeRoute || this.indexTemplate()}
26+
{this.props.activeRoute() || this.indexTemplate()}
2727
</div>
2828
</div>
2929
);
@@ -42,6 +42,7 @@ var State = React.createClass({
4242
render: function() {
4343
return (
4444
<div className="State">
45+
<h1>{this.state.name}</h1>
4546
<img src={this.imageUrl()}/>
4647
</div>
4748
);

0 commit comments

Comments
 (0)