Skip to content

Commit

Permalink
[fixed] use context in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
agundermann committed Mar 21, 2015
1 parent 69c4f67 commit 1b2293b
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 40 deletions.
12 changes: 7 additions & 5 deletions examples/animations/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// TODO: animations aren't happening, not sure what the problem is
var React = require('react');
var TransitionGroup = require('react/lib/ReactCSSTransitionGroup');
var Router = require('react-router');
var { Route, RouteHandler, Link } = Router;

var App = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render: function () {
var name = this.getRoutes().slice(0).reverse()[0].name;
var name = this.context.router.getCurrentRoutes().slice(0).reverse()[0].name;

return (
<div>
Expand Down Expand Up @@ -48,8 +50,8 @@ var Page2 = React.createClass({

var routes = (
<Route handler={App}>
<Route name="page1" handler={Page1} addHandlerKey={true} />
<Route name="page2" handler={Page2} addHandlerKey={true} />
<Route name="page1" handler={Page1} />
<Route name="page2" handler={Page2} />
</Route>
);

Expand Down
16 changes: 10 additions & 6 deletions examples/auth-flow/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var React = require('react');
var Router = require('react-router');
var { Route, RouteHandler, Link, State } = Router;
var { Route, RouteHandler, Link } = Router;

var App = React.createClass({
getInitialState: function () {
Expand Down Expand Up @@ -65,7 +65,10 @@ var Dashboard = React.createClass({
});

var Login = React.createClass({
mixins: [ Router.Navigation, State],

contextTypes: {
router: React.PropTypes.func.isRequired
},

getInitialState: function () {
return {
Expand All @@ -75,17 +78,18 @@ var Login = React.createClass({

handleSubmit: function (event) {
event.preventDefault();
var nextPath = this.getQuery().nextPath;
var { router } = this.context;
var nextPath = router.getCurrentQuery().nextPath;
var email = this.refs.email.getDOMNode().value;
var pass = this.refs.pass.getDOMNode().value;
auth.login(email, pass, function (loggedIn) {
if (!loggedIn)
return this.setState({ error: true });

if (nextPath) {
this.replaceWith(nextPath);
router.replaceWith(nextPath);
} else {
this.replaceWith('/about');
router.replaceWith('/about');
}
}.bind(this));
},
Expand Down Expand Up @@ -164,7 +168,7 @@ function pretendRequest(email, pass, cb) {
if (email === 'joe@example.com' && pass === 'password1') {
cb({
authenticated: true,
token: Math.random().toString(36).substring(7),
token: Math.random().toString(36).substring(7)
});
} else {
cb({authenticated: false});
Expand Down
15 changes: 10 additions & 5 deletions examples/data-flow/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var { Route, RouteHandler, Link } = Router;

var App = React.createClass({

mixins: [ Router.Navigation ],
contextTypes: {
router: React.PropTypes.func.isRequired
},

getInitialState: function () {
return {
Expand All @@ -28,7 +30,7 @@ var App = React.createClass({
return taco.name != removedTaco;
});
this.setState({tacos: tacos});
this.transitionTo('/');
this.context.router.transitionTo('/');
},

render: function () {
Expand All @@ -54,16 +56,19 @@ var App = React.createClass({
});

var Taco = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

remove: function () {
this.props.onRemoveTaco(this.getParams().name);
this.props.onRemoveTaco(this.context.router.getCurrentParams().name);
},

render: function () {
return (
<div className="Taco">
<h1>{this.getParams().name}</h1>
<h1>{this.context.router.getCurrentParams().name}</h1>
<button onClick={this.remove}>remove</button>
</div>
);
Expand Down
14 changes: 10 additions & 4 deletions examples/dynamic-segments/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ var App = React.createClass({
});

var User = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render () {
var { userId } = this.getParams();
var { userId } = this.context.router.getCurrentParams();
return (
<div className="User">
<h1>User id: {userId}</h1>
Expand All @@ -36,10 +39,13 @@ var User = React.createClass({


var Task = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render () {
var { userId, taskId } = this.getParams();
var { userId, taskId } = this.context.router.getCurrentParams();
return (
<div className="Task">
<h2>User id: {userId}</h2>
Expand Down
17 changes: 11 additions & 6 deletions examples/master-detail/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ var Index = React.createClass({

var Contact = React.createClass({

mixins: [ Router.Navigation, Router.State ],
contextTypes: {
router: React.PropTypes.func.isRequired
},

getStateFromStore: function () {
var id = this.getParams().id;
var id = this.context.router.getCurrentParams().id;
return {
contact: ContactStore.getContact(id)
};
Expand Down Expand Up @@ -101,9 +103,10 @@ var Contact = React.createClass({
},

destroy: function () {
var id = this.getParams().id;
var { router } = this.context;
var id = router.getCurrentParams().id;
ContactStore.removeContact(id);
this.transitionTo('/');
router.transitionTo('/');
},

render: function () {
Expand All @@ -122,15 +125,17 @@ var Contact = React.createClass({

var NewContact = React.createClass({

mixins: [ Router.Navigation ],
contextTypes: {
router: React.PropTypes.func.isRequired
},

createContact: function (event) {
event.preventDefault();
ContactStore.addContact({
first: this.refs.first.getDOMNode().value,
last: this.refs.last.getDOMNode().value
}, function (contact) {
this.transitionTo('contact', { id: contact.id });
this.context.router.transitionTo('contact', { id: contact.id });
}.bind(this));
},

Expand Down
10 changes: 7 additions & 3 deletions examples/query-params/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ var App = React.createClass({
});

var User = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render: function () {
var age = this.getQuery().showAge ? '33' : '';
var userID = this.getParams().userID;
var { router } = this.context;
var age = router.getCurrentQuery().showAge ? '33' : '';
var userID = router.getCurrentParams().userID;
return (
<div className="User">
<h1>User id: {userID}</h1>
Expand Down
7 changes: 5 additions & 2 deletions examples/rx/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ var App = React.createClass({
});

var User = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render () {
var { userId } = this.getParams();
var { userId } = this.context.router.getCurrentParams();
return (
<div className="User">
<h1>User id: {userId}</h1>
Expand Down
14 changes: 10 additions & 4 deletions examples/sidebar/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ var Sidebar = React.createClass({
});

var App = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render: function () {
var activeCategory = this.getParams().category;
var activeCategory = this.context.router.getCurrentParams().category;
return (
<div>
<Sidebar activeCategory={activeCategory} categories={data.getAll()}/>
Expand All @@ -89,10 +92,13 @@ var App = React.createClass({
});

var Item = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

render: function () {
var params = this.getParams();
var params = this.context.router.getCurrentParams();
var category = data.lookupCategory(params.category);
var item = data.lookupItem(params.category, params.name);
return (
Expand Down
7 changes: 5 additions & 2 deletions examples/simple-master-detail/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ var Index = React.createClass({
});

var State = React.createClass({
mixins: [ Router.State ],

contextTypes: {
router: React.PropTypes.func.isRequired
},

imageUrl: function (name) {
return "http://www.50states.com/maps/" + underscore(name) + ".gif";
},

render: function () {
var unitedState = findState(this.getParams().abbr);
var unitedState = findState(this.context.router.getCurrentParams().abbr);
return (
<div className="State">
<h1>{unitedState.name}</h1>
Expand Down
6 changes: 4 additions & 2 deletions examples/transitions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ var Dashboard = React.createClass({

var Form = React.createClass({

mixins: [ Router.Navigation ],
contextTypes: {
router: React.PropTypes.func.isRequired
},

statics: {
willTransitionFrom: function (transition, element) {
Expand All @@ -45,7 +47,7 @@ var Form = React.createClass({
handleSubmit: function (event) {
event.preventDefault();
this.refs.userInput.getDOMNode().value = '';
this.transitionTo('/');
this.context.router.transitionTo('/');
},

render: function () {
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {

module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader' }
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }
]
},

Expand Down

0 comments on commit 1b2293b

Please sign in to comment.