Skip to content

Commit d57f830

Browse files
committedOct 8, 2014
[changed] Public interface for Location objects
Also, removed the Flux dispatcher. Fixes #363
1 parent 360a00d commit d57f830

File tree

8 files changed

+53
-110
lines changed

8 files changed

+53
-110
lines changed
 

‎modules/actions/LocationActions.js

-5
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

-18
This file was deleted.

‎modules/locations/HashLocation.js

+8-23
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

+9-25
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

+2-16
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

+3-2
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

+31-20
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

-1
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)
Please sign in to comment.