@@ -44,36 +44,84 @@ describe('router', function () {
4444 expect ( elt . text ( ) ) . toBe ( 'Home' ) ;
4545 } ) ) ;
4646
47- function registerComponent ( name , options ) {
48- var controller = options . controller || function ( ) { } ;
4947
50- [ '$onActivate' , '$onDeactivate' , '$onReuse' , '$canReuse' , '$canDeactivate' ] . forEach ( function ( hookName ) {
51- if ( options [ hookName ] ) {
52- controller . prototype [ hookName ] = options [ hookName ] ;
53- }
48+ it ( 'should bind the component to the current router' , inject ( function ( $location ) {
49+ var router ;
50+ registerComponent ( 'homeCmp' , {
51+ bindings : { router : '=' } ,
52+ controller : function ( $scope , $element ) {
53+ this . $routerOnActivate = function ( ) {
54+ router = this . router ;
55+ } ;
56+ } ,
57+ template : 'Home'
5458 } ) ;
5559
60+ registerComponent ( 'app' , {
61+ template : '<div ng-outlet></div>' ,
62+ $routeConfig : [
63+ { path : '/' , component : 'homeCmp' }
64+ ]
65+ } ) ;
66+
67+ compile ( '<app></app>' ) ;
68+
69+ $location . path ( '/' ) ;
70+ $rootScope . $digest ( ) ;
71+ var homeElement = elt . find ( 'home-cmp' ) ;
72+ expect ( homeElement . text ( ) ) . toBe ( 'Home' ) ;
73+ expect ( homeElement . isolateScope ( ) . $ctrl . router ) . toBeDefined ( ) ;
74+ expect ( router ) . toBeDefined ( ) ;
75+ } ) ) ;
76+
77+ function registerDirective ( name , options ) {
5678 function factory ( ) {
5779 return {
5880 template : options . template || '' ,
5981 controllerAs : name ,
60- controller : controller
82+ controller : getController ( options )
6183 } ;
6284 }
85+ applyStaticProperties ( factory , options ) ;
86+ $compileProvider . directive ( name , factory ) ;
87+ }
6388
64- if ( options . $canActivate ) {
65- factory . $canActivate = options . $canActivate ;
66- }
67- if ( options . $routeConfig ) {
68- factory . $routeConfig = options . $routeConfig ;
69- }
89+ function registerComponent ( name , options ) {
7090
71- $compileProvider . directive ( name , factory ) ;
91+ var definition = {
92+ bindings : options . bindings ,
93+ template : options . template || '' ,
94+ controller : getController ( options ) ,
95+ }
96+ applyStaticProperties ( definition , options ) ;
97+ $compileProvider . component ( name , definition ) ;
7298 }
7399
74100 function compile ( template ) {
75101 elt = $compile ( '<div>' + template + '</div>' ) ( $rootScope ) ;
76102 $rootScope . $digest ( ) ;
77103 return elt ;
78104 }
79- } ) ;
105+
106+ function getController ( options ) {
107+ var controller = options . controller || function ( ) { } ;
108+ [
109+ '$routerOnActivate' , '$routerOnDeactivate' ,
110+ '$routerOnReuse' , '$routerCanReuse' ,
111+ '$routerCanDeactivate'
112+ ] . forEach ( function ( hookName ) {
113+ if ( options [ hookName ] ) {
114+ controller . prototype [ hookName ] = options [ hookName ] ;
115+ }
116+ } ) ;
117+ return controller ;
118+ }
119+
120+ function applyStaticProperties ( target , options ) {
121+ [ '$canActivate' , '$routeConfig' ] . forEach ( function ( property ) {
122+ if ( options [ property ] ) {
123+ target [ property ] = options [ property ] ;
124+ }
125+ } ) ;
126+ }
127+ } ) ;
0 commit comments