1
+ var React = require ( 'react' ) ;
2
+ var invariant = require ( 'react/lib/invariant' ) ;
1
3
var canUseDOM = require ( 'react/lib/ExecutionEnvironment' ) . canUseDOM ;
2
4
var HashLocation = require ( '../locations/HashLocation' ) ;
3
5
var HistoryLocation = require ( '../locations/HistoryLocation' ) ;
4
6
var RefreshLocation = require ( '../locations/RefreshLocation' ) ;
5
- var PathStore = require ( '../stores/PathStore' ) ;
6
7
var supportsHistory = require ( '../utils/supportsHistory' ) ;
8
+ var PathStore = require ( '../stores/PathStore' ) ;
7
9
8
10
/**
9
11
* A hash of { name: location } pairs.
@@ -16,29 +18,22 @@ var NAMED_LOCATIONS = {
16
18
17
19
/**
18
20
* A mixin for components that need to know the current URL path. Components
19
- * that use it may specify a `location` prop that they use to track changes
20
- * to the URL. They also get:
21
+ * that use it get two things:
21
22
*
22
- * 1. An `updatePath` method that is called when the
23
+ * 1. An optional `location` prop that they use to track
24
+ * changes to the URL
25
+ * 2. An `updatePath` method that is called when the
23
26
* current URL path changes
24
- * 2. A `getCurrentPath` method they can use to get
25
- * the current URL path
26
27
*
27
28
* Example:
28
29
*
29
30
* var PathWatcher = React.createClass({
30
31
*
31
32
* mixins: [ Router.PathState ],
32
33
*
33
- * getInitialState: function () {
34
- * return {
35
- * currentPath: this.getCurrentPath()
36
- * };
37
- * },
38
- *
39
- * updatePath: function () {
34
+ * updatePath: function (path, actionType) {
40
35
* this.setState({
41
- * currentPath: this.getCurrentPath()
36
+ * currentPath: path
42
37
* });
43
38
* }
44
39
*
@@ -48,6 +43,8 @@ var PathState = {
48
43
49
44
propTypes : {
50
45
46
+ fixedPath : React . PropTypes . string ,
47
+
51
48
location : function ( props , propName , componentName ) {
52
49
var location = props [ propName ] ;
53
50
@@ -59,8 +56,8 @@ var PathState = {
59
56
60
57
getDefaultProps : function ( ) {
61
58
return {
62
- location : canUseDOM ? HashLocation : null ,
63
- path : null
59
+ fixedPath : null ,
60
+ location : canUseDOM ? HashLocation : null
64
61
} ;
65
62
} ,
66
63
@@ -84,11 +81,16 @@ var PathState = {
84
81
componentWillMount : function ( ) {
85
82
var location = this . getLocation ( ) ;
86
83
84
+ invariant (
85
+ this . props . fixedPath == null || this . getLocation ( ) == null ,
86
+ 'You cannot use a fixed path with a location. Choose one or the other'
87
+ ) ;
88
+
87
89
if ( location && location . setup )
88
90
location . setup ( ) ;
89
91
90
92
if ( this . updatePath )
91
- this . updatePath ( this . getCurrentPath ( ) , this ) ;
93
+ this . updatePath ( this . getCurrentPath ( ) , this . getCurrentActionType ( ) ) ;
92
94
} ,
93
95
94
96
componentDidMount : function ( ) {
@@ -99,16 +101,17 @@ var PathState = {
99
101
PathStore . removeChangeListener ( this . handlePathChange ) ;
100
102
} ,
101
103
102
- handlePathChange : function ( sender ) {
104
+ handlePathChange : function ( ) {
103
105
if ( this . isMounted ( ) && this . updatePath )
104
- this . updatePath ( this . getCurrentPath ( ) , sender ) ;
106
+ this . updatePath ( this . getCurrentPath ( ) , this . getCurrentActionType ( ) ) ;
105
107
} ,
106
108
107
- /**
108
- * Returns the current URL path.
109
- */
110
109
getCurrentPath : function ( ) {
111
- return PathStore . getCurrentPath ( ) ;
110
+ return this . props . fixedPath || PathStore . getCurrentPath ( ) ;
111
+ } ,
112
+
113
+ getCurrentActionType : function ( ) {
114
+ return PathStore . getCurrentActionType ( ) ;
112
115
}
113
116
114
117
} ;
0 commit comments