Skip to content

Remove RouteStore #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 4, 2014
5 changes: 4 additions & 1 deletion examples/data-flow/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var Routes = Router.Routes;
var Link = Router.Link;

var App = React.createClass({

mixins: [ Router.Transitions ],

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

render: function() {
Expand Down
12 changes: 9 additions & 3 deletions examples/master-detail/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ var Index = React.createClass({
});

var Contact = React.createClass({

mixins: [ Router.Transitions ],

getStateFromStore: function(props) {
props = props || this.props;
return {
Expand Down Expand Up @@ -164,7 +167,7 @@ var Contact = React.createClass({

destroy: function() {
ContactStore.removeContact(this.props.params.id);
Router.transitionTo('/');
this.transitionTo('/');
},

render: function() {
Expand All @@ -182,14 +185,17 @@ var Contact = React.createClass({
});

var NewContact = React.createClass({

mixins: [ Router.Transitions ],

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

render: function() {
Expand Down
5 changes: 4 additions & 1 deletion examples/transitions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var Dashboard = React.createClass({
});

var Form = React.createClass({

mixins: [ Router.Transitions ],

statics: {
willTransitionFrom: function(transition, component) {
if (component.refs.userInput.getDOMNode().value !== '') {
Expand All @@ -39,7 +42,7 @@ var Form = React.createClass({
handleSubmit: function(event) {
event.preventDefault();
this.refs.userInput.getDOMNode().value = '';
Router.transitionTo('/');
this.transitionTo('/');
},

render: function() {
Expand Down
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module.exports = function(config) {
frameworks: ['mocha', 'browserify'],

files: [
'specs/main.js'
'tests.js'
],

exclude: [],

preprocessors: {
'specs/main.js': ['browserify']
'tests.js': ['browserify']
},

browserify: {
Expand Down
Binary file removed modules/.DS_Store
Binary file not shown.
128 changes: 10 additions & 118 deletions modules/actions/LocationActions.js
Original file line number Diff line number Diff line change
@@ -1,135 +1,27 @@
var supportsHistory = require('../utils/supportsHistory');
var HistoryLocation = require('../locations/HistoryLocation');
var RefreshLocation = require('../locations/RefreshLocation');
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var ActionTypes = require('../constants/ActionTypes');
var warning = require('react/lib/warning');
var isAbsoluteURL = require('../utils/isAbsoluteURL');
var makePath = require('../utils/makePath');

function loadURL(url) {
window.location = url;
}

var _location = null;
var _isDispatching = false;
var _previousPath = null;

function dispatchAction(actionType, operation) {
if (_isDispatching)
throw new Error('Cannot handle ' + actionType + ' in the middle of another action.');

_isDispatching = true;

var scrollPosition = {
x: window.scrollX,
y: window.scrollY
};

if (typeof operation === 'function')
operation(_location);

var path = _location.getCurrentPath();
LocationDispatcher.handleViewAction({
type: actionType,
path: path,
scrollPosition: scrollPosition
});

_isDispatching = false;
_previousPath = path;
}

function handleChange() {
var path = _location.getCurrentPath();

// Ignore changes inside or caused by dispatchAction
if (!_isDispatching && path !== _previousPath) {
dispatchAction(ActionTypes.POP);
}
}

/**
* Actions that modify the URL.
*/
var LocationActions = {

getLocation: function () {
return _location;
},

setup: function (location) {
// When using HistoryLocation, automatically fallback
// to RefreshLocation in browsers that do not support
// the HTML5 history API.
if (location === HistoryLocation && !supportsHistory())
location = RefreshLocation;

if (_location !== null) {
warning(
_location === location,
'Cannot use location %s, already using %s', location, _location
);
return;
}

_location = location;

if (_location !== null) {
dispatchAction(ActionTypes.SETUP, function (location) {
if (typeof location.setup === 'function')
location.setup(handleChange);
});
}
},

teardown: function () {
if (_location !== null) {
if (typeof _location.teardown === 'function')
_location.teardown();

_location = null;
}
},
/**
* Indicates a location is being setup for the first time.
*/
SETUP: 'setup',

/**
* Transitions to the URL specified in the arguments by pushing
* a new URL onto the history stack.
* Indicates a new location is being pushed to the history stack.
*/
transitionTo: function (to, params, query) {
if (isAbsoluteURL(to)) {
loadURL(to);
} else {
dispatchAction(ActionTypes.PUSH, function (location) {
var path = makePath(to, params, query);
location.push(path);
});
}
},
PUSH: 'push',

/**
* Transitions to the URL specified in the arguments by replacing
* the current URL in the history stack.
* Indicates the current location should be replaced.
*/
replaceWith: function (to, params, query) {
if (isAbsoluteURL(to)) {
loadURL(to);
} else {
dispatchAction(ActionTypes.REPLACE, function (location) {
var path = makePath(to, params, query);
location.replace(path);
});
}
},
REPLACE: 'replace',

/**
* Transitions to the previous URL.
* Indicates the most recent entry should be removed from the history stack.
*/
goBack: function () {
dispatchAction(ActionTypes.POP, function (location) {
location.pop();
});
}
POP: 'pop'

};

Expand Down
Loading