From 478a25ed27f15e3806c11e4c1b82293ae562a30a Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Sun, 1 Nov 2015 14:31:36 -0600 Subject: [PATCH] refactor(angular_1_router): Added value for router root component Closes #4965 Closes #5052 --- .../angular1_router/src/module_template.js | 16 ++-- .../test/integration/router_spec.js | 79 +++++++++++++++++++ 2 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 modules/angular1_router/test/integration/router_spec.js diff --git a/modules/angular1_router/src/module_template.js b/modules/angular1_router/src/module_template.js index 6712ca77c8..43415af4d5 100644 --- a/modules/angular1_router/src/module_template.js +++ b/modules/angular1_router/src/module_template.js @@ -1,9 +1,12 @@ angular.module('ngComponentRouter'). value('$route', null). // can be overloaded with ngRouteShim - factory('$router', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', routerFactory]); + // Because Angular 1 has no notion of a root component, we use an object with unique identity + // to represent this. Can be overloaded with a component name + value('$routerRootComponent', new Object()). + factory('$router', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', '$routerRootComponent', routerFactory]); -function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector) { +function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector, $routerRootComponent) { // When this file is processed, the line below is replaced with // the contents of `../lib/facades.es5`. @@ -42,12 +45,7 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc var RouteRegistry = exports.RouteRegistry; var RootRouter = exports.RootRouter; - - // Because Angular 1 has no notion of a root component, we use an object with unique identity - // to represent this. - var ROOT_COMPONENT_OBJECT = new Object(); - - var registry = new RouteRegistry(ROOT_COMPONENT_OBJECT); + var registry = new RouteRegistry($routerRootComponent); var location = new Location(); $$directiveIntrospector(function (name, factory) { @@ -58,7 +56,7 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc } }); - var router = new RootRouter(registry, location, ROOT_COMPONENT_OBJECT); + var router = new RootRouter(registry, location, $routerRootComponent); $rootScope.$watch(function () { return $location.path(); }, function (path) { if (router.lastNavigationAttempt !== path) { router.navigateByUrl(path); diff --git a/modules/angular1_router/test/integration/router_spec.js b/modules/angular1_router/test/integration/router_spec.js new file mode 100644 index 0000000000..c302c21cea --- /dev/null +++ b/modules/angular1_router/test/integration/router_spec.js @@ -0,0 +1,79 @@ +'use strict'; + +describe('router', function () { + + var elt, + $compile, + $rootScope, + $router, + $compileProvider; + + beforeEach(function () { + module('ng'); + module('ngComponentRouter'); + module(function($provide) { + $provide.value('$routerRootComponent', 'app'); + }); + module(function (_$compileProvider_) { + $compileProvider = _$compileProvider_; + }); + + inject(function (_$compile_, _$rootScope_, _$router_) { + $compile = _$compile_; + $rootScope = _$rootScope_; + $router = _$router_; + }); + }); + + it('should work with a provided root component', inject(function($location) { + registerComponent('homeCmp', { + template: 'Home' + }); + + registerComponent('app', { + template: '
', + $routeConfig: [ + { path: '/', component: 'homeCmp' } + ] + }); + + compile(''); + + $location.path('/'); + $rootScope.$digest(); + expect(elt.text()).toBe('Home'); + })); + + function registerComponent(name, options) { + var controller = options.controller || function () {}; + + ['$onActivate', '$onDeactivate', '$onReuse', '$canReuse', '$canDeactivate'].forEach(function (hookName) { + if (options[hookName]) { + controller.prototype[hookName] = options[hookName]; + } + }); + + function factory() { + return { + template: options.template || '', + controllerAs: name, + controller: controller + }; + } + + if (options.$canActivate) { + factory.$canActivate = options.$canActivate; + } + if (options.$routeConfig) { + factory.$routeConfig = options.$routeConfig; + } + + $compileProvider.directive(name, factory); + } + + function compile(template) { + elt = $compile('
' + template + '
')($rootScope); + $rootScope.$digest(); + return elt; + } +});