| OLD | NEW |
| 1 part of angular.routing; | 1 part of angular.routing; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * A directive that works with the [Router] and loads the template associated | 4 * A directive that works with the [Router] and loads the template associated |
| 5 * with the current route. | 5 * with the current route. |
| 6 * | 6 * |
| 7 * <ng-view></ng-view> | 7 * <ng-view></ng-view> |
| 8 * | 8 * |
| 9 * [NgViewDirective] can work with [NgViewDirective] to define nested views | 9 * [NgViewDirective] can work with [NgViewDirective] to define nested views |
| 10 * for hierarchical routes. For example: | 10 * for hierarchical routes. For example: |
| 11 * | 11 * |
| 12 * void initRoutes(Router router, ViewFactory view) { | 12 * class MyRouteInitializer implements RouteInitializer { |
| 13 * router.root | 13 * void init(Router router, ViewFactory view) { |
| 14 * ..addRoute( | 14 * router.root |
| 15 * name: 'library', | 15 * ..addRoute( |
| 16 * path: '/library', | 16 * name: 'library', |
| 17 * enter: view('library.html'), | 17 * path: '/library', |
| 18 * mount: (Route route) => route | 18 * enter: view('library.html'), |
| 19 * ..addRoute( | 19 * mount: (Route route) => route |
| 20 * name: 'all', | 20 * ..addRoute( |
| 21 * path: '/all', | 21 * name: 'all', |
| 22 * enter: view('book_list.html')) | 22 * path: '/all', |
| 23 * ..addRoute( | 23 * enter: view('book_list.html')) |
| 24 * name: 'book', | 24 * ..addRoute( |
| 25 * path: '/:bookId', | 25 * name: 'book', |
| 26 * mount: (Route route) => route | 26 * path: '/:bookId', |
| 27 * ..addRoute( | 27 * mount: (Route route) => route |
| 28 * name: 'overview', | 28 * ..addRoute( |
| 29 * path: '/overview', | 29 * name: 'overview', |
| 30 * defaultRoute: true, | 30 * path: '/overview', |
| 31 * enter: view('book_overview.html')) | 31 * defaultRoute: true, |
| 32 * ..addRoute( | 32 * enter: view('book_overview.html')) |
| 33 * name: 'read', | 33 * ..addRoute( |
| 34 * path: '/read', | 34 * name: 'read', |
| 35 * enter: view('book_read.html')))); | 35 * path: '/read', |
| 36 * enter: view('book_read.html')))); |
| 37 * } |
| 36 * } | 38 * } |
| 37 * } | |
| 38 * | 39 * |
| 39 * index.html: | 40 * index.html: |
| 40 * | 41 * |
| 41 * <ng-view></ng-view> | 42 * <ng-view></ng-view> |
| 42 * | 43 * |
| 43 * library.html: | 44 * library.html: |
| 44 * | 45 * |
| 45 * <div> | 46 * <div> |
| 46 * <h1>Library!</h1> | 47 * <h1>Library!</h1> |
| 47 * | 48 * |
| 48 * <ng-view></ng-view> | 49 * <ng-view></ng-view> |
| 49 * </div> | 50 * </div> |
| 50 * | 51 * |
| 51 * book_list.html: | 52 * book_list.html: |
| 52 * | 53 * |
| 53 * <ul> | 54 * <ul> |
| 54 * <li><a href="/library/12345/overview">Book 12345</a> | 55 * <li><a href="/library/12345/overview">Book 12345</a> |
| 55 * <li><a href="/library/23456/overview">Book 23456</a> | 56 * <li><a href="/library/23456/overview">Book 23456</a> |
| 56 * </ul> | 57 * </ul> |
| 57 */ | 58 */ |
| 58 @NgDirective( | 59 @NgDirective( |
| 59 selector: 'ng-view', | 60 selector: 'ng-view', |
| 60 publishTypes: const [RouteProvider], | 61 publishTypes: const [RouteProvider], |
| 61 visibility: NgDirective.CHILDREN_VISIBILITY) | 62 visibility: NgDirective.CHILDREN_VISIBILITY |
| 63 ) |
| 62 class NgViewDirective implements NgDetachAware, RouteProvider { | 64 class NgViewDirective implements NgDetachAware, RouteProvider { |
| 63 final NgRoutingHelper locationService; | 65 final NgRoutingHelper locationService; |
| 64 final BlockCache blockCache; | 66 final BlockCache blockCache; |
| 67 final Scope scope; |
| 65 final Injector injector; | 68 final Injector injector; |
| 66 final Element element; | 69 final Element element; |
| 67 final Scope scope; | |
| 68 RouteHandle _route; | 70 RouteHandle _route; |
| 69 | 71 |
| 70 Block _previousBlock; | 72 Block _previousBlock; |
| 71 Scope _previousScope; | 73 Scope _previousScope; |
| 72 Route _viewRoute; | 74 Route _viewRoute; |
| 73 | 75 |
| 74 NgViewDirective(this.element, this.blockCache, | 76 NgViewDirective(this.element, this.blockCache, this.scope, Injector injector,
Router router) |
| 75 Injector injector, Router router, | 77 : injector = injector, locationService = injector.get(NgRoutingHelper) { |
| 76 this.scope) | |
| 77 : injector = injector, | |
| 78 locationService = injector.get(NgRoutingHelper) | |
| 79 { | |
| 80 RouteProvider routeProvider = injector.parent.get(NgViewDirective); | 78 RouteProvider routeProvider = injector.parent.get(NgViewDirective); |
| 81 _route = routeProvider != null ? | 79 if (routeProvider != null) { |
| 82 routeProvider.route.newHandle() : | 80 _route = routeProvider.route.newHandle(); |
| 83 router.root.newHandle(); | 81 } else { |
| 82 _route = router.root.newHandle(); |
| 83 } |
| 84 locationService._registerPortal(this); | 84 locationService._registerPortal(this); |
| 85 _maybeReloadViews(); | 85 _maybeReloadViews(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void _maybeReloadViews() { | 88 void _maybeReloadViews() { |
| 89 if (_route.isActive) locationService._reloadViews(startingFrom: _route); | 89 if (_route.isActive) { |
| 90 locationService._reloadViews(startingFrom: _route); |
| 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 detach() { | 94 detach() { |
| 93 _route.discard(); | 95 _route.discard(); |
| 94 locationService._unregisterPortal(this); | 96 locationService._unregisterPortal(this); |
| 95 } | 97 } |
| 96 | 98 |
| 97 _show(String templateUrl, Route route, List<Module> modules) { | 99 _show(String templateUrl, Route route) { |
| 98 assert(route.isActive); | 100 assert(route.isActive); |
| 99 | 101 |
| 100 if (_viewRoute != null) return; | 102 if (_viewRoute != null) return; |
| 101 _viewRoute = route; | 103 _viewRoute = route; |
| 102 | 104 |
| 103 StreamSubscription _leaveSubscription; | 105 StreamSubscription _leaveSubscription; |
| 104 _leaveSubscription = route.onLeave.listen((_) { | 106 _leaveSubscription = route.onLeave.listen((_) { |
| 105 _leaveSubscription.cancel(); | 107 _leaveSubscription.cancel(); |
| 106 _leaveSubscription = null; | 108 _leaveSubscription = null; |
| 107 _viewRoute = null; | 109 _viewRoute = null; |
| 108 _cleanUp(); | 110 _cleanUp(); |
| 109 }); | 111 }); |
| 110 | 112 |
| 111 var viewInjector = injector; | 113 blockCache.fromUrl(templateUrl).then((blockFactory) { |
| 112 if (modules != null) { | |
| 113 viewInjector = forceNewDirectivesAndFilters(viewInjector, modules); | |
| 114 } | |
| 115 | |
| 116 var newDirectives = viewInjector.get(DirectiveMap); | |
| 117 blockCache.fromUrl(templateUrl, newDirectives).then((blockFactory) { | |
| 118 _cleanUp(); | 114 _cleanUp(); |
| 119 _previousScope = scope.createChild(new PrototypeMap(scope.context)); | 115 _previousScope = scope.$new(); |
| 120 _previousBlock = blockFactory( | 116 _previousBlock = blockFactory( |
| 121 viewInjector.createChild( | 117 injector.createChild([new Module()..value(Scope, _previousScope)])); |
| 122 [new Module()..value(Scope, _previousScope)])); | |
| 123 | 118 |
| 124 _previousBlock.elements.forEach((elm) => element.append(elm)); | 119 _previousBlock.elements.forEach((elm) => element.append(elm)); |
| 125 }); | 120 }); |
| 126 } | 121 } |
| 127 | 122 |
| 128 _cleanUp() { | 123 _cleanUp() { |
| 129 if (_previousBlock == null) return; | 124 if (_previousBlock == null) { |
| 125 return; |
| 126 } |
| 130 | 127 |
| 131 _previousBlock.remove(); | 128 _previousBlock.remove(); |
| 132 _previousScope.destroy(); | 129 _previousScope.$destroy(); |
| 133 | 130 |
| 134 _previousBlock = null; | 131 _previousBlock = null; |
| 135 _previousScope = null; | 132 _previousScope = null; |
| 136 } | 133 } |
| 137 | 134 |
| 138 Route get route => _viewRoute; | 135 Route get route => _viewRoute; |
| 139 String get routeName => _viewRoute.name; | 136 String get routeName => _viewRoute.name; |
| 140 Map<String, String> get parameters { | 137 Map<String, String> get parameters { |
| 141 var res = <String, String>{}; | 138 var res = <String, String>{}; |
| 142 var p = _viewRoute; | 139 var p = _viewRoute; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 /** | 189 /** |
| 193 * Returns the name of the current route. | 190 * Returns the name of the current route. |
| 194 */ | 191 */ |
| 195 String get routeName; | 192 String get routeName; |
| 196 | 193 |
| 197 /** | 194 /** |
| 198 * Returns parameters for this route. | 195 * Returns parameters for this route. |
| 199 */ | 196 */ |
| 200 Map<String, String> get parameters; | 197 Map<String, String> get parameters; |
| 201 } | 198 } |
| OLD | NEW |