refactor(Directive): drop moduleId

moduleId is only used by components to resolve urls.
Directives have no templates and do not need moduleId.
Closes #5873
This commit is contained in:
Victor Berchet 2015-12-13 17:35:33 -08:00
parent 1c779d8b9e
commit 5a04ffec3e
9 changed files with 37 additions and 47 deletions

View File

@ -32,12 +32,13 @@ export class RuntimeMetadataResolver {
var meta = this._cache.get(directiveType); var meta = this._cache.get(directiveType);
if (isBlank(meta)) { if (isBlank(meta)) {
var dirMeta = this._directiveResolver.resolve(directiveType); var dirMeta = this._directiveResolver.resolve(directiveType);
var moduleUrl = calcModuleUrl(directiveType, dirMeta); var moduleUrl = null;
var templateMeta = null; var templateMeta = null;
var changeDetectionStrategy = null; var changeDetectionStrategy = null;
if (dirMeta instanceof md.ComponentMetadata) { if (dirMeta instanceof md.ComponentMetadata) {
var cmpMeta = <md.ComponentMetadata>dirMeta; var cmpMeta = <md.ComponentMetadata>dirMeta;
moduleUrl = calcModuleUrl(directiveType, cmpMeta);
var viewMeta = this._viewResolver.resolve(directiveType); var viewMeta = this._viewResolver.resolve(directiveType);
templateMeta = new cpl.CompileTemplateMetadata({ templateMeta = new cpl.CompileTemplateMetadata({
encapsulation: viewMeta.encapsulation, encapsulation: viewMeta.encapsulation,
@ -107,8 +108,8 @@ function isValidDirective(value: Type): boolean {
return isPresent(value) && (value instanceof Type); return isPresent(value) && (value instanceof Type);
} }
function calcModuleUrl(type: Type, dirMeta: md.DirectiveMetadata): string { function calcModuleUrl(type: Type, cmpMetadata: md.ComponentMetadata): string {
var moduleId = dirMeta.moduleId; var moduleId = cmpMetadata.moduleId;
if (isPresent(moduleId)) { if (isPresent(moduleId)) {
var scheme = getUrlScheme(moduleId); var scheme = getUrlScheme(moduleId);
return isPresent(scheme) && scheme.length > 0 ? moduleId : return isPresent(scheme) && scheme.length > 0 ? moduleId :

View File

@ -132,7 +132,6 @@ export class DirectiveResolver {
outputs: mergedOutputs, outputs: mergedOutputs,
host: mergedHost, host: mergedHost,
exportAs: dm.exportAs, exportAs: dm.exportAs,
moduleId: dm.moduleId,
queries: mergedQueries, queries: mergedQueries,
providers: dm.providers providers: dm.providers
}); });

View File

@ -27,7 +27,6 @@ class Directive extends DirectiveMetadata {
List bindings, List bindings,
List providers, List providers,
String exportAs, String exportAs,
String moduleId,
Map<String, dynamic> queries}) Map<String, dynamic> queries})
: super( : super(
selector: selector, selector: selector,
@ -39,7 +38,6 @@ class Directive extends DirectiveMetadata {
bindings: bindings, bindings: bindings,
providers: providers, providers: providers,
exportAs: exportAs, exportAs: exportAs,
moduleId: moduleId,
queries: queries); queries: queries);
} }

View File

@ -146,7 +146,6 @@ export interface DirectiveFactory {
bindings?: any[], bindings?: any[],
providers?: any[], providers?: any[],
exportAs?: string, exportAs?: string,
moduleId?: string,
queries?: {[key: string]: any} queries?: {[key: string]: any}
}): DirectiveDecorator; }): DirectiveDecorator;
new (obj: { new (obj: {
@ -159,7 +158,6 @@ export interface DirectiveFactory {
bindings?: any[], bindings?: any[],
providers?: any[], providers?: any[],
exportAs?: string, exportAs?: string,
moduleId?: string,
queries?: {[key: string]: any} queries?: {[key: string]: any}
}): DirectiveMetadata; }): DirectiveMetadata;
} }

View File

@ -696,26 +696,6 @@ export class DirectiveMetadata extends InjectableMetadata {
*/ */
exportAs: string; exportAs: string;
/**
* The module id of the module that contains the directive.
* Needed to be able to resolve relative urls for templates and styles.
* In Dart, this can be determined automatically and does not need to be set.
* In CommonJS, this can always be set to `module.id`.
*
* ## Simple Example
*
* ```
* @Directive({
* selector: 'someDir',
* moduleId: module.id
* })
* class SomeDir {
* }
*
* ```
*/
moduleId: string;
// TODO: add an example after ContentChildren and ViewChildren are in master // TODO: add an example after ContentChildren and ViewChildren are in master
/** /**
* Configures the queries that will be injected into the directive. * Configures the queries that will be injected into the directive.
@ -752,7 +732,7 @@ export class DirectiveMetadata extends InjectableMetadata {
queries: {[key: string]: any}; queries: {[key: string]: any};
constructor({selector, inputs, outputs, properties, events, host, bindings, providers, exportAs, constructor({selector, inputs, outputs, properties, events, host, bindings, providers, exportAs,
moduleId, queries}: { queries}: {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
@ -762,7 +742,6 @@ export class DirectiveMetadata extends InjectableMetadata {
providers?: any[], providers?: any[],
/** @deprecated */ bindings?: any[], /** @deprecated */ bindings?: any[],
exportAs?: string, exportAs?: string,
moduleId?: string,
queries?: {[key: string]: any} queries?: {[key: string]: any}
} = {}) { } = {}) {
super(); super();
@ -773,7 +752,6 @@ export class DirectiveMetadata extends InjectableMetadata {
this._events = events; this._events = events;
this.host = host; this.host = host;
this.exportAs = exportAs; this.exportAs = exportAs;
this.moduleId = moduleId;
this.queries = queries; this.queries = queries;
this._providers = providers; this._providers = providers;
this._bindings = bindings; this._bindings = bindings;
@ -865,6 +843,26 @@ export class ComponentMetadata extends DirectiveMetadata {
private _viewProviders: any[]; private _viewProviders: any[];
private _viewBindings: any[]; private _viewBindings: any[];
/**
* The module id of the module that contains the component.
* Needed to be able to resolve relative urls for templates and styles.
* In Dart, this can be determined automatically and does not need to be set.
* In CommonJS, this can always be set to `module.id`.
*
* ## Simple Example
*
* ```
* @Directive({
* selector: 'someDir',
* moduleId: module.id
* })
* class SomeDir {
* }
*
* ```
*/
moduleId: string;
templateUrl: string; templateUrl: string;
template: string; template: string;
@ -913,7 +911,6 @@ export class ComponentMetadata extends DirectiveMetadata {
events: events, events: events,
host: host, host: host,
exportAs: exportAs, exportAs: exportAs,
moduleId: moduleId,
bindings: bindings, bindings: bindings,
providers: providers, providers: providers,
queries: queries queries: queries
@ -929,6 +926,7 @@ export class ComponentMetadata extends DirectiveMetadata {
this.directives = directives; this.directives = directives;
this.pipes = pipes; this.pipes = pipes;
this.encapsulation = encapsulation; this.encapsulation = encapsulation;
this.moduleId = moduleId;
} }
} }

View File

@ -47,7 +47,6 @@ export class MockDirectiveResolver extends DirectiveResolver {
host: dm.host, host: dm.host,
providers: providers, providers: providers,
exportAs: dm.exportAs, exportAs: dm.exportAs,
moduleId: dm.moduleId,
queries: dm.queries queries: dm.queries
}); });
} }

View File

@ -70,10 +70,10 @@ export function main() {
it('should use the moduleUrl from the reflector if none is given', it('should use the moduleUrl from the reflector if none is given',
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => { inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
var value: string = resolver.getMetadata(DirectiveWithoutModuleId).type.moduleUrl; var value: string = resolver.getMetadata(ComponentWithoutModuleId).type.moduleUrl;
var expectedEndValue = var expectedEndValue =
IS_DART ? 'base/dist/dart/angular2/test/compiler/runtime_metadata_spec.dart' : './'; IS_DART ? 'base/dist/dart/angular2/test/compiler/runtime_metadata_spec.dart' : './';
expect((<any>value).endsWith(expectedEndValue)).toBe(true); expect(value.endsWith(expectedEndValue)).toBe(true);
})); }));
}); });
@ -82,7 +82,7 @@ export function main() {
it('should return the directive metadatas', it('should return the directive metadatas',
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => { inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything)) expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
.toEqual([resolver.getMetadata(DirectiveWithoutModuleId)]); .toEqual([resolver.getMetadata(SomeDirective)]);
})); }));
describe("platform directives", () => { describe("platform directives", () => {
@ -91,10 +91,7 @@ export function main() {
it('should include platform directives when available', it('should include platform directives when available',
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => { inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything)) expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
.toEqual([ .toEqual([resolver.getMetadata(ADirective), resolver.getMetadata(SomeDirective)]);
resolver.getMetadata(ADirective),
resolver.getMetadata(DirectiveWithoutModuleId)
]);
})); }));
}); });
}); });
@ -102,14 +99,16 @@ export function main() {
}); });
} }
@Directive({selector: 'a-directive'}) @Directive({selector: 'a-directive'})
class ADirective { class ADirective {
} }
@Directive({selector: 'someSelector'}) @Directive({selector: 'someSelector'})
class DirectiveWithoutModuleId { class SomeDirective {
}
@Component({selector: 'someComponent', template: ''})
class ComponentWithoutModuleId {
} }
@Component({ @Component({
@ -131,7 +130,7 @@ class DirectiveWithoutModuleId {
encapsulation: ViewEncapsulation.Emulated, encapsulation: ViewEncapsulation.Emulated,
styles: ['someStyle'], styles: ['someStyle'],
styleUrls: ['someStyleUrl'], styleUrls: ['someStyleUrl'],
directives: [DirectiveWithoutModuleId] directives: [SomeDirective]
}) })
class ComponentWithEverything implements OnChanges, class ComponentWithEverything implements OnChanges,
OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit, OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit,

View File

@ -321,7 +321,7 @@ class CompWithEmbeddedTemplate {
} }
@Directive({selector: 'plain', moduleId: THIS_MODULE_ID}) @Directive({selector: 'plain'})
@View({template: ''}) @View({template: ''})
class NonComponent { class NonComponent {
} }

View File

@ -923,7 +923,6 @@ var NG_CORE = [
'Directive.events', 'Directive.events',
'Directive.exportAs', 'Directive.exportAs',
'Directive.host', 'Directive.host',
'Directive.moduleId',
'Directive.inputs', 'Directive.inputs',
'Directive.properties', 'Directive.properties',
'Directive.queries', 'Directive.queries',
@ -935,7 +934,6 @@ var NG_CORE = [
'DirectiveMetadata.events', 'DirectiveMetadata.events',
'DirectiveMetadata.exportAs', 'DirectiveMetadata.exportAs',
'DirectiveMetadata.host', 'DirectiveMetadata.host',
'DirectiveMetadata.moduleId',
'DirectiveMetadata.inputs', 'DirectiveMetadata.inputs',
'DirectiveMetadata.properties', 'DirectiveMetadata.properties',
'DirectiveMetadata.queries', 'DirectiveMetadata.queries',