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