UNPKG

react-crossroads

Version:

Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.

61 lines (56 loc) 1.46 kB
var BlockRouting, React, getNextBlockId; React = require('react'); getNextBlockId = (function() { var nextBlockId; nextBlockId = 1; return function() { return nextBlockId++; }; })(); BlockRouting = { contextTypes: { router: React.PropTypes.object.isRequired }, getInitialState: function() { return { BlockRouting: { blockId: null, blocked: false } }; }, componentWillMount: function() { return this.context.router.stores.location.addChangeListener(this.handleLocationStateChange); }, componentWillUnmount: function() { return this.context.router.stores.location.removeChangeListener(this.handleLocationStateChange); }, handleLocationStateChange: function() { if (this.isMounted()) { return this.setState({ BlockRouting: { blockId: this.state.BlockRouting.blockId, blocked: this.context.router.stores.location.isBlocked() } }); } }, toggleBlock: function() { if (this.state.BlockRouting.blocked) { this.context.router.actions.unblock(this.state.BlockRouting.blockId); return this.setState({ BlockRouting: { blockId: null } }); } else { this.setState({ BlockRouting: { blockId: getNextBlockId() } }); return this.context.router.actions.block(this.state.BlockRouting.blockId); } } }; module.exports = BlockRouting;