react-crossroads
Version:
Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.
71 lines (65 loc) • 2.28 kB
JavaScript
var LocationAttempt, Logger, React, RouterConstants;
React = require('react');
RouterConstants = require('../constants/RouterConstants');
Logger = require('../utils/logger');
LocationAttempt = {
contextTypes: {
router: React.PropTypes.object.isRequired
},
getInitialState: function() {
return {
LocationAttempt: {
attemptDispatchToken: this.context.router.dispatcher.register(this.locationAttemptDispatcherHandler)
}
};
},
componentWillUnmount: function() {
if (this.state.LocationAttempt.attemptDispatchToken != null) {
return this.context.router.dispatcher.unregister(this.state.LocationAttempt.attemptDispatchToken);
}
},
locationAttemptDispatcherHandler: function(payload) {
var lastAttempt, _ref, _ref1;
if (!this.isMounted()) {
return;
}
this.context.router.dispatcher.waitFor([this.context.router.stores.location.dispatchToken]);
if (!(this.context.router.stores.location.isBlocked() || (payload != null ? (_ref = payload.action) != null ? _ref.actionType : void 0 : void 0) === RouterConstants.LOCATION_UNBLOCK)) {
this.dismissLocationAttempt();
}
if ((payload != null ? (_ref1 = payload.action) != null ? _ref1.actionType : void 0 : void 0) !== RouterConstants.LOCATION_ATTEMPT) {
return;
}
lastAttempt = payload.action.originalAction;
lastAttempt.fromLocationEvent = false;
this.setState({
LocationAttempt: {
lastAttempt: lastAttempt
}
});
if (this.handleLocationAttempt) {
return this.handleLocationAttempt();
}
},
dismissLocationAttempt: function() {
this.setState({
LocationAttempt: {
lastAttempt: null
}
});
if (this.handleLocationAttemptDismissed) {
return this.handleLocationAttemptDismissed();
}
},
continueLocationAttempt: function() {
if (this.state.LocationAttempt.lastAttempt == null) {
return;
}
if (this.context.router.stores.location.isBlocked()) {
Logger.development.warn('Continuing transition when the location store is still blocked!');
}
this.dismissLocationAttempt();
return this.context.router.actions._dispatch(this.state.LocationAttempt.lastAttempt);
}
};
module.exports = LocationAttempt;