1
- var ActiveStore = require ( '../stores/ActiveStore' ) ;
1
+ var React = require ( 'react' ) ;
2
+ var ActiveDelegate = require ( './ActiveDelegate' ) ;
2
3
3
4
/**
4
5
* A mixin for components that need to know about the routes, params,
5
6
* and query that are currently active. Components that use it get two
6
7
* things:
7
8
*
8
- * 1. An `isActive` static method they can use to check if a route,
9
- * params, and query are active.
10
- * 2. An `updateActiveState` instance method that is called when the
9
+ * 1. An `updateActiveState` method that is called when the
11
10
* active state changes.
11
+ * 2. An `isActive` method they can use to check if a route,
12
+ * params, and query are active.
13
+ *
12
14
*
13
15
* Example:
14
16
*
@@ -24,40 +26,45 @@ var ActiveStore = require('../stores/ActiveStore');
24
26
*
25
27
* updateActiveState: function () {
26
28
* this.setState({
27
- * isActive: Tab .isActive(routeName, params, query)
29
+ * isActive: this .isActive(routeName, params, query)
28
30
* })
29
31
* }
30
32
*
31
33
* });
32
34
*/
33
35
var ActiveState = {
34
36
35
- statics : {
36
-
37
- /**
38
- * Returns true if the route with the given name, URL parameters, and query
39
- * are all currently active.
40
- */
41
- isActive : ActiveStore . isActive
42
-
37
+ contextTypes : {
38
+ activeDelegate : React . PropTypes . any . isRequired
43
39
} ,
44
40
45
- componentWillMount : function ( ) {
46
- ActiveStore . addChangeListener ( this . handleActiveStateChange ) ;
41
+ /**
42
+ * Returns this component's ActiveDelegate component.
43
+ */
44
+ getActiveDelegate : function ( ) {
45
+ return this . context . activeDelegate ;
47
46
} ,
48
47
49
48
componentDidMount : function ( ) {
50
- if ( this . updateActiveState )
51
- this . updateActiveState ( ) ;
49
+ this . getActiveDelegate ( ) . addChangeListener ( this . handleActiveStateChange ) ;
50
+ this . handleActiveStateChange ( ) ;
52
51
} ,
53
52
54
53
componentWillUnmount : function ( ) {
55
- ActiveStore . removeChangeListener ( this . handleActiveStateChange ) ;
54
+ this . getActiveDelegate ( ) . removeChangeListener ( this . handleActiveStateChange ) ;
56
55
} ,
57
56
58
57
handleActiveStateChange : function ( ) {
59
58
if ( this . isMounted ( ) && typeof this . updateActiveState === 'function' )
60
59
this . updateActiveState ( ) ;
60
+ } ,
61
+
62
+ /**
63
+ * Returns true if the route with the given name, URL parameters, and
64
+ * query are all currently active.
65
+ */
66
+ isActive : function ( routeName , params , query ) {
67
+ return this . getActiveDelegate ( ) . isActive ( routeName , params , query ) ;
61
68
}
62
69
63
70
} ;
0 commit comments