feat(angular1_router): allow component to bind to router

This commit is contained in:
Peter Bacon Darwin 2016-02-07 20:40:00 +00:00 committed by Igor Minar
parent c6036435f0
commit 0f22dce036
2 changed files with 66 additions and 17 deletions

View File

@ -156,10 +156,11 @@ function ngOutletDirective($animate, $q: ng.IQService, $router) {
this.controller.$$routeParams = instruction.params; this.controller.$$routeParams = instruction.params;
this.controller.$$template = this.controller.$$template =
'<' + dashCase(componentName) + '></' + dashCase(componentName) + '>'; '<' + dashCase(componentName) + ' router="$$router"></' + dashCase(componentName) + '>';
this.controller.$$router = this.router.childRouter(instruction.componentType); this.controller.$$router = this.router.childRouter(instruction.componentType);
let newScope = scope.$new(); let newScope = scope.$new();
newScope.$$router = this.controller.$$router;
let clone = $transclude(newScope, clone => { let clone = $transclude(newScope, clone => {
$animate.enter(clone, null, this.currentElement || element); $animate.enter(clone, null, this.currentElement || element);

View File

@ -44,36 +44,84 @@ describe('router', function () {
expect(elt.text()).toBe('Home'); expect(elt.text()).toBe('Home');
})); }));
function registerComponent(name, options) {
var controller = options.controller || function () {};
['$onActivate', '$onDeactivate', '$onReuse', '$canReuse', '$canDeactivate'].forEach(function (hookName) { it('should bind the component to the current router', inject(function($location) {
if (options[hookName]) { var router;
controller.prototype[hookName] = options[hookName]; registerComponent('homeCmp', {
} bindings: { router: '=' },
controller: function($scope, $element) {
this.$routerOnActivate = function() {
router = this.router;
};
},
template: 'Home'
}); });
registerComponent('app', {
template: '<div ng-outlet></div>',
$routeConfig: [
{ path: '/', component: 'homeCmp' }
]
});
compile('<app></app>');
$location.path('/');
$rootScope.$digest();
var homeElement = elt.find('home-cmp');
expect(homeElement.text()).toBe('Home');
expect(homeElement.isolateScope().$ctrl.router).toBeDefined();
expect(router).toBeDefined();
}));
function registerDirective(name, options) {
function factory() { function factory() {
return { return {
template: options.template || '', template: options.template || '',
controllerAs: name, controllerAs: name,
controller: controller controller: getController(options)
}; };
} }
applyStaticProperties(factory, options);
if (options.$canActivate) {
factory.$canActivate = options.$canActivate;
}
if (options.$routeConfig) {
factory.$routeConfig = options.$routeConfig;
}
$compileProvider.directive(name, factory); $compileProvider.directive(name, factory);
} }
function registerComponent(name, options) {
var definition = {
bindings: options.bindings,
template: options.template || '',
controller: getController(options),
}
applyStaticProperties(definition, options);
$compileProvider.component(name, definition);
}
function compile(template) { function compile(template) {
elt = $compile('<div>' + template + '</div>')($rootScope); elt = $compile('<div>' + template + '</div>')($rootScope);
$rootScope.$digest(); $rootScope.$digest();
return elt; return elt;
} }
});
function getController(options) {
var controller = options.controller || function () {};
[
'$routerOnActivate', '$routerOnDeactivate',
'$routerOnReuse', '$routerCanReuse',
'$routerCanDeactivate'
].forEach(function (hookName) {
if (options[hookName]) {
controller.prototype[hookName] = options[hookName];
}
});
return controller;
}
function applyStaticProperties(target, options) {
['$canActivate', '$routeConfig'].forEach(function(property) {
if (options[property]) {
target[property] = options[property];
}
});
}
});