Skip to content

Commit

Permalink
feat(core/directives): add support for ngComponentRouter with ng1 met…
Browse files Browse the repository at this point in the history
…hod names
  • Loading branch information
Hotell committed May 29, 2016
1 parent add0f0a commit 393f440
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/core/directives/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export interface DirectiveCtrl extends AfterContentInit, AfterContentChecked, Af
}

export interface NgmDirective extends ng.IDirective {
_ngOnInitBound?():void;
_ngOnInitBound?():void,
$canActivate?(): boolean|ng.IPromise<boolean>,
}
32 changes: 28 additions & 4 deletions src/core/directives/directive_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,34 @@ export class DirectiveProvider {
// legacy property overrides all generated DDO stuff
const ddo = this._createDDO( _ddo, metadata.legacy );

return [
directiveName,
function directiveFactory() { return ddo }
]
function directiveFactory() { return ddo }

// ==========================
// ngComponentRouter Support:
// ==========================

// @TODO(pete) remove the following `forEach` before we release 1.6.0
// The [email protected] looks for the annotations on the controller constructor
// Nothing in Angular looks for annotations on the factory function but we can't remove
// it from 1.5.x yet.

// Copy any annotation properties (starting with $) over to the factory and controller constructor functions
// These could be used by libraries such as the new component router
StringMapWrapper.forEach( ddo as any, function ( val: any, key: string ) {
if ( key.charAt( 0 ) === '$' ) {
directiveFactory[ key ] = val;
// Don't try to copy over annotations to named controller
if ( isFunction( ddo.controller ) ) { ddo.controller[ key ] = val }
}
} );
// support componentRouter $canActivate lc hook as static instead of defined within legacy object
// componentRouter reads all lc hooks from directiveFactory ¯\_(ツ)_/¯
// @TODO update this when new component router will be available for Angular 1 ( 1.6 release probably )
if ( isFunction( (type as any).$canActivate ) ) {
(directiveFactory as any).$canActivate = (type as any).$canActivate;
}

return [ directiveName, directiveFactory ]

}

Expand Down
31 changes: 21 additions & 10 deletions src/core/directives/metadata_directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ import { Type } from '../../facade/lang';
import { InjectableMetadata } from '../di/metadata';
import { ChangeDetectionStrategy } from '../change_detection/constants';

export type RouteConfig = RouteDefinition[];
export type RouteDefinition = {
path: string,
name: string,
component?: string,
loader?: any,
redirectTo?: any,
useAsDefault?: boolean,
data?: any
}
export type LegacyDirectiveDefinition = {
compile?: ng.IDirectiveCompileFn;
controllerAs?: string;
priority?: number;
replace?: boolean;
scope?: any;
template?: any;
templateNamespace?: string;
templateUrl?: any;
terminal?: boolean;
transclude?: any;
compile?: ng.IDirectiveCompileFn,
controllerAs?: string,
priority?: number,
replace?: boolean,
scope?: any,
template?: any,
templateNamespace?: string,
templateUrl?: any,
terminal?: boolean,
transclude?: any,
$routeConfig?: RouteConfig,
}
/**
* Directives allow you to attach behavior to elements in the DOM.
Expand Down

0 comments on commit 393f440

Please sign in to comment.