Skip to content

Commit 3aa4130

Browse files
committed
Use ES6 class/object notation
1 parent 0d5353c commit 3aa4130

File tree

5 files changed

+92
-80
lines changed

5 files changed

+92
-80
lines changed

modules/locations/HashLocation.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function onHashChange() {
6060
*/
6161
var HashLocation = {
6262

63-
addChangeListener: function (listener) {
63+
addChangeListener(listener) {
6464
_changeListeners.push(listener);
6565

6666
// Do this BEFORE listening for hashchange.
@@ -77,7 +77,7 @@ var HashLocation = {
7777
}
7878
},
7979

80-
removeChangeListener: function(listener) {
80+
removeChangeListener(listener) {
8181
_changeListeners = _changeListeners.filter(function (l) {
8282
return l !== listener;
8383
});
@@ -93,26 +93,26 @@ var HashLocation = {
9393
}
9494
},
9595

96-
push: function (path) {
96+
push(path) {
9797
_actionType = LocationActions.PUSH;
9898
window.location.hash = path;
9999
},
100100

101-
replace: function (path) {
101+
replace(path) {
102102
_actionType = LocationActions.REPLACE;
103103
window.location.replace(
104104
window.location.pathname + window.location.search + '#' + path
105105
);
106106
},
107107

108-
pop: function () {
108+
pop() {
109109
_actionType = LocationActions.POP;
110110
History.back();
111111
},
112112

113113
getCurrentPath: getHashPath,
114114

115-
toString: function () {
115+
toString() {
116116
return '<HashLocation>';
117117
}
118118

modules/locations/HistoryLocation.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function onPopState(event) {
3737
*/
3838
var HistoryLocation = {
3939

40-
addChangeListener: function (listener) {
40+
addChangeListener(listener) {
4141
_changeListeners.push(listener);
4242

4343
if (!_isListening) {
@@ -51,7 +51,7 @@ var HistoryLocation = {
5151
}
5252
},
5353

54-
removeChangeListener: function(listener) {
54+
removeChangeListener(listener) {
5555
_changeListeners = _changeListeners.filter(function (l) {
5656
return l !== listener;
5757
});
@@ -67,13 +67,13 @@ var HistoryLocation = {
6767
}
6868
},
6969

70-
push: function (path) {
70+
push(path) {
7171
window.history.pushState({ path: path }, '', path);
7272
History.length += 1;
7373
notifyChange(LocationActions.PUSH);
7474
},
7575

76-
replace: function (path) {
76+
replace(path) {
7777
window.history.replaceState({ path: path }, '', path);
7878
notifyChange(LocationActions.REPLACE);
7979
},
@@ -82,7 +82,7 @@ var HistoryLocation = {
8282

8383
getCurrentPath: getWindowPath,
8484

85-
toString: function () {
85+
toString() {
8686
return '<HistoryLocation>';
8787
}
8888

modules/locations/RefreshLocation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ var History = require('../History');
88
*/
99
var RefreshLocation = {
1010

11-
push: function (path) {
11+
push(path) {
1212
window.location = path;
1313
},
1414

15-
replace: function (path) {
15+
replace(path) {
1616
window.location.replace(path);
1717
},
1818

1919
pop: History.back,
2020

2121
getCurrentPath: HistoryLocation.getCurrentPath,
2222

23-
toString: function () {
23+
toString() {
2424
return '<RefreshLocation>';
2525
}
2626

modules/locations/StaticLocation.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ function throwCannotModify() {
99
* stateless environments like servers where there is no path history,
1010
* only the path that was used in the request.
1111
*/
12-
function StaticLocation(path) {
13-
this.path = path;
12+
class StaticLocation {
13+
14+
constructor(path) {
15+
this.path = path;
16+
}
17+
18+
getCurrentPath() {
19+
return this.path;
20+
}
21+
22+
toString() {
23+
return `<StaticLocation path="${this.path}">`;
24+
}
25+
1426
}
1527

28+
// TODO: Include these in the above class definition
29+
// once we can use ES7 property initializers.
1630
StaticLocation.prototype.push = throwCannotModify;
1731
StaticLocation.prototype.replace = throwCannotModify;
1832
StaticLocation.prototype.pop = throwCannotModify;
1933

20-
StaticLocation.prototype.getCurrentPath = function () {
21-
return this.path;
22-
};
23-
24-
StaticLocation.prototype.toString = function () {
25-
return '<StaticLocation path="' + this.path + '">';
26-
};
27-
2834
module.exports = StaticLocation;

modules/locations/TestLocation.js

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,68 @@ var History = require('../History');
55
/**
66
* A location that is convenient for testing and does not require a DOM.
77
*/
8-
function TestLocation(history) {
9-
this.history = history || [];
10-
this.listeners = [];
11-
this._updateHistoryLength();
12-
}
8+
class TestLocation {
9+
10+
constructor(history) {
11+
this.history = history || [];
12+
this.listeners = [];
13+
this._updateHistoryLength();
14+
}
15+
16+
get needsDOM() {
17+
return false;
18+
}
19+
20+
_updateHistoryLength() {
21+
History.length = this.history.length;
22+
}
23+
24+
_notifyChange(type) {
25+
for (var i = 0, len = this.listeners.length; i < len; ++i)
26+
this.listeners[i].call(this, { path: this.getCurrentPath(), type: type });
27+
}
28+
29+
addChangeListener(listener) {
30+
this.listeners.push(listener);
31+
}
32+
33+
removeChangeListener(listener) {
34+
this.listeners = this.listeners.filter(function (l) {
35+
return l !== listener;
36+
});
37+
}
38+
39+
push(path) {
40+
this.history.push(path);
41+
this._updateHistoryLength();
42+
this._notifyChange(LocationActions.PUSH);
43+
}
1344

14-
TestLocation.prototype.needsDOM = false;
15-
16-
TestLocation.prototype._updateHistoryLength = function () {
17-
History.length = this.history.length;
18-
};
19-
20-
TestLocation.prototype._notifyChange = function (type) {
21-
for (var i = 0, len = this.listeners.length; i < len; ++i)
22-
this.listeners[i].call(this, { path: this.getCurrentPath(), type: type });
23-
};
24-
25-
TestLocation.prototype.addChangeListener = function (listener) {
26-
this.listeners.push(listener);
27-
};
28-
29-
TestLocation.prototype.removeChangeListener = function (listener) {
30-
this.listeners = this.listeners.filter(function (l) {
31-
return l !== listener;
32-
});
33-
};
34-
35-
TestLocation.prototype.push = function (path) {
36-
this.history.push(path);
37-
this._updateHistoryLength();
38-
this._notifyChange(LocationActions.PUSH);
39-
};
40-
41-
TestLocation.prototype.replace = function (path) {
42-
invariant(
43-
this.history.length,
44-
'You cannot replace the current path with no history'
45-
);
46-
47-
this.history[this.history.length - 1] = path;
48-
49-
this._notifyChange(LocationActions.REPLACE);
50-
};
51-
52-
TestLocation.prototype.pop = function () {
53-
this.history.pop();
54-
this._updateHistoryLength();
55-
this._notifyChange(LocationActions.POP);
56-
};
57-
58-
TestLocation.prototype.getCurrentPath = function () {
59-
return this.history[this.history.length - 1];
60-
};
61-
62-
TestLocation.prototype.toString = function () {
63-
return '<TestLocation>';
64-
};
45+
replace(path) {
46+
invariant(
47+
this.history.length,
48+
'You cannot replace the current path with no history'
49+
);
50+
51+
this.history[this.history.length - 1] = path;
52+
53+
this._notifyChange(LocationActions.REPLACE);
54+
}
55+
56+
pop() {
57+
this.history.pop();
58+
this._updateHistoryLength();
59+
this._notifyChange(LocationActions.POP);
60+
}
61+
62+
getCurrentPath() {
63+
return this.history[this.history.length - 1];
64+
}
65+
66+
toString() {
67+
return '<TestLocation>';
68+
}
69+
70+
}
6571

6672
module.exports = TestLocation;

0 commit comments

Comments
 (0)