Skip to content

Commit c976d21

Browse files
committed
Merge branch 'no-dispatcher'
2 parents acc00f7 + 723545a commit c976d21

File tree

9 files changed

+63
-132
lines changed

9 files changed

+63
-132
lines changed

docs/api/misc/Location.md

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ following methods must be implemented:
77
Methods
88
-------
99

10-
### `setup()`
10+
### `setup(onChange)`
1111

12-
Called when the router is first setup.
12+
Called when the router is first setup. The `onChange` function should be
13+
called without any arguments when the location changes.
1314

1415
### `teardown`
1516

@@ -28,30 +29,17 @@ another.
2829

2930
Called when the router attempts to go back one entry in the history.
3031

32+
### `getCurrentPath`
33+
34+
Should return the current URL path, complete with query string (if applicable).
35+
This method should be ready to go immediately after setup.
36+
3137
### `toString`
3238

3339
Should return a useful string for logging and debugging.
3440

3541
Example
3642
-------
3743

38-
This is a terrible example, you're probably better off looking at the
39-
implementations in this repository.
40-
41-
```js
42-
var MyLocation = {
43-
44-
setup: function () {},
45-
46-
teardown: function () {},
47-
48-
push: function (path) {},
49-
50-
replace: function (path) {},
51-
52-
pop: function () {},
53-
54-
toString: function () {}
55-
56-
};
57-
```
44+
For examples of how to implement your own location, please see the locations
45+
included in this repository.

modules/actions/LocationActions.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
*/
44
var LocationActions = {
55

6-
/**
7-
* Indicates a location is being setup for the first time.
8-
*/
9-
SETUP: 'setup',
10-
116
/**
127
* Indicates a new location is being pushed to the history stack.
138
*/

modules/dispatchers/LocationDispatcher.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

modules/locations/HashLocation.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
var invariant = require('react/lib/invariant');
2-
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
31
var LocationActions = require('../actions/LocationActions');
4-
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
52
var getWindowPath = require('../utils/getWindowPath');
63

74
function getHashPath() {
@@ -21,11 +18,13 @@ function ensureSlash() {
2118
return false;
2219
}
2320

21+
var _onChange;
22+
2423
function onHashChange() {
2524
if (ensureSlash()) {
2625
var path = getHashPath();
2726

28-
LocationDispatcher.handleViewAction({
27+
_onChange({
2928
// If we don't have an _actionType then all we know is the hash
3029
// changed. It was probably caused by the user clicking the Back
3130
// button, but may have also been the Forward button or manual
@@ -38,36 +37,22 @@ function onHashChange() {
3837
}
3938
}
4039

41-
var _isSetup = false;
42-
4340
/**
4441
* A Location that uses `window.location.hash`.
4542
*/
4643
var HashLocation = {
4744

48-
setup: function () {
49-
if (_isSetup)
50-
return;
51-
52-
invariant(
53-
canUseDOM,
54-
'You cannot use HashLocation in an environment with no DOM'
55-
);
45+
setup: function (onChange) {
46+
_onChange = onChange;
5647

48+
// Do this BEFORE listening for hashchange.
5749
ensureSlash();
5850

59-
LocationDispatcher.handleViewAction({
60-
type: LocationActions.SETUP,
61-
path: getHashPath()
62-
});
63-
6451
if (window.addEventListener) {
6552
window.addEventListener('hashchange', onHashChange, false);
6653
} else {
6754
window.attachEvent('onhashchange', onHashChange);
6855
}
69-
70-
_isSetup = true;
7156
},
7257

7358
teardown: function () {
@@ -76,8 +61,6 @@ var HashLocation = {
7661
} else {
7762
window.detachEvent('onhashchange', onHashChange);
7863
}
79-
80-
_isSetup = false;
8164
},
8265

8366
push: function (path) {
@@ -95,6 +78,8 @@ var HashLocation = {
9578
window.history.back();
9679
},
9780

81+
getCurrentPath: getHashPath,
82+
9883
toString: function () {
9984
return '<HashLocation>';
10085
}

modules/locations/HistoryLocation.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,28 @@
1-
var invariant = require('react/lib/invariant');
2-
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
31
var LocationActions = require('../actions/LocationActions');
4-
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
52
var getWindowPath = require('../utils/getWindowPath');
63

4+
var _onChange;
5+
76
function onPopState() {
8-
LocationDispatcher.handleViewAction({
7+
_onChange({
98
type: LocationActions.POP,
109
path: getWindowPath()
1110
});
1211
}
1312

14-
var _isSetup = false;
15-
1613
/**
1714
* A Location that uses HTML5 history.
1815
*/
1916
var HistoryLocation = {
2017

21-
setup: function () {
22-
if (_isSetup)
23-
return;
24-
25-
invariant(
26-
canUseDOM,
27-
'You cannot use HistoryLocation in an environment with no DOM'
28-
);
29-
30-
LocationDispatcher.handleViewAction({
31-
type: LocationActions.SETUP,
32-
path: getWindowPath()
33-
});
18+
setup: function (onChange) {
19+
_onChange = onChange;
3420

3521
if (window.addEventListener) {
3622
window.addEventListener('popstate', onPopState, false);
3723
} else {
3824
window.attachEvent('popstate', onPopState);
3925
}
40-
41-
_isSetup = true;
4226
},
4327

4428
teardown: function () {
@@ -47,14 +31,12 @@ var HistoryLocation = {
4731
} else {
4832
window.detachEvent('popstate', onPopState);
4933
}
50-
51-
_isSetup = false;
5234
},
5335

5436
push: function (path) {
5537
window.history.pushState({ path: path }, '', path);
5638

57-
LocationDispatcher.handleViewAction({
39+
_onChange({
5840
type: LocationActions.PUSH,
5941
path: getWindowPath()
6042
});
@@ -63,7 +45,7 @@ var HistoryLocation = {
6345
replace: function (path) {
6446
window.history.replaceState({ path: path }, '', path);
6547

66-
LocationDispatcher.handleViewAction({
48+
_onChange({
6749
type: LocationActions.REPLACE,
6850
path: getWindowPath()
6951
});
@@ -73,6 +55,8 @@ var HistoryLocation = {
7355
window.history.back();
7456
},
7557

58+
getCurrentPath: getWindowPath,
59+
7660
toString: function () {
7761
return '<HistoryLocation>';
7862
}

modules/locations/RefreshLocation.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
var invariant = require('react/lib/invariant');
2-
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
3-
var LocationActions = require('../actions/LocationActions');
4-
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
51
var getWindowPath = require('../utils/getWindowPath');
62

73
/**
@@ -11,18 +7,6 @@ var getWindowPath = require('../utils/getWindowPath');
117
*/
128
var RefreshLocation = {
139

14-
setup: function () {
15-
invariant(
16-
canUseDOM,
17-
'You cannot use RefreshLocation in an environment with no DOM'
18-
);
19-
20-
LocationDispatcher.handleViewAction({
21-
type: LocationActions.SETUP,
22-
path: getWindowPath()
23-
});
24-
},
25-
2610
push: function (path) {
2711
window.location = path;
2812
},
@@ -35,6 +19,8 @@ var RefreshLocation = {
3519
window.history.back();
3620
},
3721

22+
getCurrentPath: getWindowPath,
23+
3824
toString: function () {
3925
return '<RefreshLocation>';
4026
}

modules/mixins/LocationContext.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
44
var HashLocation = require('../locations/HashLocation');
55
var HistoryLocation = require('../locations/HistoryLocation');
66
var RefreshLocation = require('../locations/RefreshLocation');
7+
var PathStore = require('../stores/PathStore');
78
var supportsHistory = require('../utils/supportsHistory');
89

910
/**
@@ -60,8 +61,8 @@ var LocationContext = {
6061
'Cannot use location without a DOM'
6162
);
6263

63-
if (location && location.setup)
64-
location.setup();
64+
if (location)
65+
PathStore.useLocation(location);
6566
},
6667

6768
/**

modules/stores/PathStore.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
var invariant = require('react/lib/invariant');
12
var EventEmitter = require('events').EventEmitter;
23
var LocationActions = require('../actions/LocationActions');
3-
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
44

55
var CHANGE_EVENT = 'change';
66
var _events = new EventEmitter;
@@ -9,7 +9,15 @@ function notifyChange() {
99
_events.emit(CHANGE_EVENT);
1010
}
1111

12-
var _currentPath, _currentActionType;
12+
var _currentLocation, _currentPath, _currentActionType;
13+
14+
function handleLocationChangeAction(action) {
15+
if (_currentPath !== action.path) {
16+
_currentPath = action.path;
17+
_currentActionType = action.type;
18+
notifyChange();
19+
}
20+
}
1321

1422
/**
1523
* The PathStore keeps track of the current URL path.
@@ -28,6 +36,26 @@ var PathStore = {
2836
_events.removeAllListeners(CHANGE_EVENT);
2937
},
3038

39+
/**
40+
* Setup the PathStore to use the given location.
41+
*/
42+
useLocation: function (location) {
43+
invariant(
44+
_currentLocation == null || _currentLocation === location,
45+
'You cannot use %s and %s on the same page',
46+
_currentLocation, location
47+
);
48+
49+
if (_currentLocation !== location) {
50+
if (location.setup)
51+
location.setup(handleLocationChangeAction);
52+
53+
_currentPath = location.getCurrentPath();
54+
}
55+
56+
_currentLocation = location;
57+
},
58+
3159
/**
3260
* Returns the current URL path.
3361
*/
@@ -40,24 +68,7 @@ var PathStore = {
4068
*/
4169
getCurrentActionType: function () {
4270
return _currentActionType;
43-
},
44-
45-
dispatchToken: LocationDispatcher.register(function (payload) {
46-
var action = payload.action;
47-
48-
switch (action.type) {
49-
case LocationActions.SETUP:
50-
case LocationActions.PUSH:
51-
case LocationActions.REPLACE:
52-
case LocationActions.POP:
53-
if (_currentPath !== action.path) {
54-
_currentPath = action.path;
55-
_currentActionType = action.type;
56-
notifyChange();
57-
}
58-
break;
59-
}
60-
})
71+
}
6172

6273
};
6374

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
},
4343
"dependencies": {
4444
"events": "1.0.1",
45-
"flux": "2.0.1",
4645
"qs": "2.2.2",
4746
"when": "3.4.6"
4847
},

0 commit comments

Comments
 (0)