refactor(angular_1_router): Added value for router root component

Closes #4965

Closes #5052
This commit is contained in:
Brandon Roberts 2015-11-01 14:31:36 -06:00 committed by Brandon
parent 1edb17b8d0
commit 478a25ed27
2 changed files with 86 additions and 9 deletions

View File

@ -1,9 +1,12 @@
angular.module('ngComponentRouter'). angular.module('ngComponentRouter').
value('$route', null). // can be overloaded with ngRouteShim 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 // When this file is processed, the line below is replaced with
// the contents of `../lib/facades.es5`. // the contents of `../lib/facades.es5`.
@ -42,12 +45,7 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc
var RouteRegistry = exports.RouteRegistry; var RouteRegistry = exports.RouteRegistry;
var RootRouter = exports.RootRouter; var RootRouter = exports.RootRouter;
var registry = new RouteRegistry($routerRootComponent);
// 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 location = new Location(); var location = new Location();
$$directiveIntrospector(function (name, factory) { $$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) { $rootScope.$watch(function () { return $location.path(); }, function (path) {
if (router.lastNavigationAttempt !== path) { if (router.lastNavigationAttempt !== path) {
router.navigateByUrl(path); router.navigateByUrl(path);

View File

@ -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: '<div ng-outlet></div>',
$routeConfig: [
{ path: '/', component: 'homeCmp' }
]
});
compile('<app></app>');
$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('<div>' + template + '</div>')($rootScope);
$rootScope.$digest();
return elt;
}
});