Skip to content

Commit 985627b

Browse files
committed
cleanup(DI): clean up visibility decorators
BREAKING CHANGE: Replace @ancestor() with @host() @SkipSelf() Replace @unbounded() wwith @SkipSelf() Replace @ancestor({self:true}) with @host() Replace @unbounded({self:true}) with nothing Replace new AncestorMetadata() with [new HostMetadata(), new SkipSelfMetadata()] Replace new UnboundedMetadata() with new SkipSelfMetadata() Replace new Ancestor({self:true}) with new HostMetadata()
1 parent a9ec6b9 commit 985627b

27 files changed

Lines changed: 246 additions & 268 deletions

File tree

modules/angular2/annotations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Annotations provide the additional information that Angular requires in order to run your
66
* application. This module
77
* contains {@link Component}, {@link Directive}, and {@link View} annotations, as well as
8-
* the {@link Ancestor} annotation that is used by Angular to resolve dependencies.
8+
* the {@link Host} annotation that is used by Angular to resolve dependencies.
99
*
1010
*/
1111

modules/angular2/di.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ export {
88
InjectMetadata,
99
OptionalMetadata,
1010
InjectableMetadata,
11-
VisibilityMetadata,
1211
SelfMetadata,
13-
AncestorMetadata,
14-
UnboundedMetadata,
15-
DependencyMetadata,
16-
DEFAULT_VISIBILITY
12+
HostMetadata,
13+
SkipSelfMetadata,
14+
DependencyMetadata
1715
} from './src/di/metadata';
1816

1917
// we have to reexport * because Dart and TS export two different sets of types

modules/angular2/docs/core/02_directives.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ Injecting other directives into directives follows a similar mechanism as inject
261261
There are five kinds of visibilities:
262262

263263
* (no annotation): Inject dependent directives only if they are on the current element.
264-
* `@ancestor`: Inject a directive if it is at any element above the current element.
264+
* `@SkipSelf()`: Inject a directive if it is at any element above the current element.
265265
* `@child`: Inject a list of direct children which match a given type. (Used with `Query`)
266266
* `@descendant`: Inject a list of any children which match a given type. (Used with `Query`)
267267

@@ -299,8 +299,8 @@ class FieldSet { |
299299
@Directive({ selector: 'field' }) |
300300
class Field { |
301301
constructor( |
302-
@ancestor field:Form, |
303-
@ancestor field:FieldSet, |
302+
@SkipSelf() field:Form, |
303+
@SkipSelf() field:FieldSet, |
304304
) { ... } |
305305
} |
306306
|
@@ -336,7 +336,7 @@ Shadow DOM provides an encapsulation for components, so as a general rule it doe
336336
})
337337
class Kid {
338338
constructor(
339-
@Ancestor() dad:Dad,
339+
@SkipSelf() dad:Dad,
340340
@Optional() grandpa:Grandpa
341341
) {
342342
this.name = 'Billy';
@@ -353,7 +353,7 @@ class Kid {
353353
directives: [Kid]
354354
})
355355
class Dad {
356-
constructor(@Ancestor() dad:Grandpa) {
356+
constructor(@SkipSelf() dad:Grandpa) {
357357
this.name = 'Joe Jr';
358358
this.dad = dad.name;
359359
}

modules/angular2/src/change_detection/pipes/pipes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ListWrapper, isListLikeIterable, StringMapWrapper} from 'angular2/src/facade/collection';
22
import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang';
33
import {Pipe, PipeFactory} from './pipe';
4-
import {Injectable, UnboundedMetadata, OptionalMetadata} from 'angular2/di';
4+
import {Injectable, OptionalMetadata, SkipSelfMetadata} from 'angular2/di';
55
import {ChangeDetectorRef} from '../change_detector_ref';
66
import {Binding} from 'angular2/di';
77

@@ -80,7 +80,7 @@ export class Pipes {
8080
return Pipes.create(config, pipes);
8181
},
8282
// Dependency technically isn't optional, but we can provide a better error message this way.
83-
deps: [[Pipes, new UnboundedMetadata(), new OptionalMetadata()]]
83+
deps: [[Pipes, new SkipSelfMetadata(), new OptionalMetadata()]]
8484
});
8585
}
8686

modules/angular2/src/core/annotations_impl/annotations.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ import {DEFAULT} from 'angular2/change_detection';
5353
*
5454
* To inject other directives, declare the constructor parameter as:
5555
* - `directive:DirectiveType`: a directive on the current element only
56-
* - `@Ancestor() directive:DirectiveType`: any directive that matches the type between the current
56+
* - `@Host() directive:DirectiveType`: any directive that matches the type between the current
5757
* element and the
58-
* Shadow DOM root. Current element is not included in the resolution, therefore even if it could
59-
* resolve it, it will
60-
* be ignored.
58+
* Shadow DOM root.
6159
* - `@Query(DirectiveType) query:QueryList<DirectiveType>`: A live collection of direct child
6260
* directives.
6361
* - `@QueryDescendants(DirectiveType) query:QueryList<DirectiveType>`: A live collection of any
@@ -164,21 +162,19 @@ import {DEFAULT} from 'angular2/change_detection';
164162
* ### Injecting a directive from any ancestor elements
165163
*
166164
* Directives can inject other directives declared on any ancestor element (in the current Shadow
167-
* DOM), i.e. on the
168-
* parent element and its parents. By definition, a directive with an `@Ancestor` annotation does
169-
* not attempt to
170-
* resolve dependencies for the current element, even if this would satisfy the dependency.
171-
*
165+
* DOM), i.e. on the current element, the
166+
* parent element, or its parents.
172167
* ```
173168
* @Directive({ selector: '[my-directive]' })
174169
* class MyDirective {
175-
* constructor(@Ancestor() dependency: Dependency) {
170+
* constructor(@Host() dependency: Dependency) {
176171
* expect(dependency.id).toEqual(2);
177172
* }
178173
* }
179174
* ```
180175
*
181-
* `@Ancestor` checks the parent, as well as its parents recursively. If `dependency="2"` didn't
176+
* `@Host` checks the current element, the parent, as well as its parents recursively. If
177+
* `dependency="2"` didn't
182178
* exist on the direct parent, this injection would
183179
* have returned
184180
* `dependency="1"`.

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
AbstractBindingError,
2626
CyclicDependencyError,
2727
resolveForwardRef,
28-
VisibilityMetadata,
2928
DependencyProvider
3029
} from 'angular2/di';
3130
import {
@@ -167,9 +166,10 @@ export class TreeNode<T extends TreeNode<any>> {
167166
}
168167

169168
export class DirectiveDependency extends Dependency {
170-
constructor(key: Key, optional: boolean, visibility: any, properties: List<any>,
171-
public attributeName: string, public queryDecorator: Query) {
172-
super(key, optional, visibility, properties);
169+
constructor(key: Key, optional: boolean, lowerBoundVisibility: Object,
170+
upperBoundVisibility: Object, properties: List<any>, public attributeName: string,
171+
public queryDecorator: Query) {
172+
super(key, optional, lowerBoundVisibility, upperBoundVisibility, properties);
173173
this._verify();
174174
}
175175

@@ -183,9 +183,9 @@ export class DirectiveDependency extends Dependency {
183183
}
184184

185185
static createFrom(d: Dependency): Dependency {
186-
return new DirectiveDependency(d.key, d.optional, d.visibility, d.properties,
187-
DirectiveDependency._attributeName(d.properties),
188-
DirectiveDependency._query(d.properties));
186+
return new DirectiveDependency(
187+
d.key, d.optional, d.lowerBoundVisibility, d.upperBoundVisibility, d.properties,
188+
DirectiveDependency._attributeName(d.properties), DirectiveDependency._query(d.properties));
189189
}
190190

191191
static _attributeName(properties): string {

modules/angular2/src/di/binding.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import {Key} from './key';
1414
import {
1515
InjectMetadata,
1616
InjectableMetadata,
17-
VisibilityMetadata,
1817
OptionalMetadata,
19-
DEFAULT_VISIBILITY,
18+
SelfMetadata,
19+
HostMetadata,
20+
SkipSelfMetadata,
2021
DependencyMetadata
2122
} from './metadata';
2223
import {NoAnnotationError} from './exceptions';
@@ -26,12 +27,10 @@ import {resolveForwardRef} from './forward_ref';
2627
* @private
2728
*/
2829
export class Dependency {
29-
constructor(public key: Key, public optional: boolean, public visibility: VisibilityMetadata,
30-
public properties: List<any>) {}
30+
constructor(public key: Key, public optional: boolean, public lowerBoundVisibility: any,
31+
public upperBoundVisibility: any, public properties: List<any>) {}
3132

32-
static fromKey(key: Key): Dependency {
33-
return new Dependency(key, false, DEFAULT_VISIBILITY, []);
34-
}
33+
static fromKey(key: Key): Dependency { return new Dependency(key, false, null, null, []); }
3534
}
3635

3736
const _EMPTY_LIST = CONST_EXPR([]);
@@ -390,50 +389,61 @@ function _dependenciesFor(typeOrFunc): List<Dependency> {
390389
return ListWrapper.map(params, (p: List<any>) => _extractToken(typeOrFunc, p, params));
391390
}
392391

393-
function _extractToken(typeOrFunc, annotations /*List<any> | any*/, params: List<List<any>>):
392+
function _extractToken(typeOrFunc, metadata /*List<any> | any*/, params: List<List<any>>):
394393
Dependency {
395394
var depProps = [];
396395
var token = null;
397396
var optional = false;
398397

399-
if (!isArray(annotations)) {
400-
return _createDependency(annotations, optional, DEFAULT_VISIBILITY, depProps);
398+
if (!isArray(metadata)) {
399+
return _createDependency(metadata, optional, null, null, depProps);
401400
}
402401

403-
var visibility = DEFAULT_VISIBILITY;
402+
var lowerBoundVisibility = null;
403+
;
404+
var upperBoundVisibility = null;
405+
;
404406

405-
for (var i = 0; i < annotations.length; ++i) {
406-
var paramAnnotation = annotations[i];
407+
for (var i = 0; i < metadata.length; ++i) {
408+
var paramMetadata = metadata[i];
407409

408-
if (paramAnnotation instanceof Type) {
409-
token = paramAnnotation;
410+
if (paramMetadata instanceof Type) {
411+
token = paramMetadata;
410412

411-
} else if (paramAnnotation instanceof InjectMetadata) {
412-
token = paramAnnotation.token;
413+
} else if (paramMetadata instanceof InjectMetadata) {
414+
token = paramMetadata.token;
413415

414-
} else if (paramAnnotation instanceof OptionalMetadata) {
416+
} else if (paramMetadata instanceof OptionalMetadata) {
415417
optional = true;
416418

417-
} else if (paramAnnotation instanceof VisibilityMetadata) {
418-
visibility = paramAnnotation;
419+
} else if (paramMetadata instanceof SelfMetadata) {
420+
upperBoundVisibility = paramMetadata;
421+
422+
} else if (paramMetadata instanceof HostMetadata) {
423+
upperBoundVisibility = paramMetadata;
419424

420-
} else if (paramAnnotation instanceof DependencyMetadata) {
421-
if (isPresent(paramAnnotation.token)) {
422-
token = paramAnnotation.token;
425+
} else if (paramMetadata instanceof SkipSelfMetadata) {
426+
lowerBoundVisibility = paramMetadata;
427+
428+
} else if (paramMetadata instanceof DependencyMetadata) {
429+
if (isPresent(paramMetadata.token)) {
430+
token = paramMetadata.token;
423431
}
424-
depProps.push(paramAnnotation);
432+
depProps.push(paramMetadata);
425433
}
426434
}
427435

428436
token = resolveForwardRef(token);
429437

430438
if (isPresent(token)) {
431-
return _createDependency(token, optional, visibility, depProps);
439+
return _createDependency(token, optional, lowerBoundVisibility, upperBoundVisibility, depProps);
432440
} else {
433441
throw new NoAnnotationError(typeOrFunc, params);
434442
}
435443
}
436444

437-
function _createDependency(token, optional, visibility, depProps): Dependency {
438-
return new Dependency(Key.get(token), optional, visibility, depProps);
445+
function _createDependency(token, optional, lowerBoundVisibility, upperBoundVisibility, depProps):
446+
Dependency {
447+
return new Dependency(Key.get(token), optional, lowerBoundVisibility, upperBoundVisibility,
448+
depProps);
439449
}

modules/angular2/src/di/decorators.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class Self extends SelfMetadata {
3232
}
3333

3434
/**
35-
* {@link AncestorMetadata}.
35+
* {@link HostMetadata}.
3636
*/
37-
class Ancestor extends AncestorMetadata {
38-
const Ancestor({bool self}) : super(self: self);
37+
class Host extends HostMetadata {
38+
const Host() : super();
3939
}
4040

4141
/**
42-
* {@link UnboundedMetadata}.
42+
* {@link SkipSelfMetadata}.
4343
*/
44-
class Unbounded extends UnboundedMetadata {
45-
const Unbounded({bool self}) : super(self: self);
44+
class SkipSelf extends SkipSelfMetadata {
45+
const SkipSelf() : super();
4646
}

modules/angular2/src/di/decorators.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import {
33
OptionalMetadata,
44
InjectableMetadata,
55
SelfMetadata,
6-
VisibilityMetadata,
7-
AncestorMetadata,
8-
UnboundedMetadata
6+
HostMetadata,
7+
SkipSelfMetadata
98
} from './metadata';
109
import {makeDecorator, makeParamDecorator, TypeDecorator} from '../util/decorators';
1110

@@ -42,19 +41,19 @@ export interface SelfFactory {
4241
}
4342

4443
/**
45-
* Factory for creating {@link AncestorMetadata}.
44+
* Factory for creating {@link HostMetadata}.
4645
*/
47-
export interface AncestorFactory {
48-
(visibility?: {self: boolean}): any;
49-
new (visibility?: {self: boolean}): AncestorMetadata;
46+
export interface HostFactory {
47+
(): any;
48+
new (): HostMetadata;
5049
}
5150

5251
/**
53-
* Factory for creating {@link UnboundedMetadata}.
52+
* Factory for creating {@link SkipSelfMetadata}.
5453
*/
55-
export interface UnboundedFactory {
56-
(visibility?: {self: boolean}): any;
57-
new (visibility?: {self: boolean}): UnboundedMetadata;
54+
export interface SkipSelfFactory {
55+
(): any;
56+
new (): SkipSelfMetadata;
5857
}
5958

6059
/**
@@ -78,11 +77,11 @@ export var Injectable: InjectableFactory = <InjectableFactory>makeDecorator(Inje
7877
export var Self: SelfFactory = makeParamDecorator(SelfMetadata);
7978

8079
/**
81-
* Factory for creating {@link AncestorMetadata}.
80+
* Factory for creating {@link HostMetadata}.
8281
*/
83-
export var Ancestor: AncestorFactory = makeParamDecorator(AncestorMetadata);
82+
export var Host: HostFactory = makeParamDecorator(HostMetadata);
8483

8584
/**
86-
* Factory for creating {@link UnboundedMetadata}.
85+
* Factory for creating {@link SkipSelfMetadata}.
8786
*/
88-
export var Unbounded: UnboundedFactory = makeParamDecorator(UnboundedMetadata);
87+
export var SkipSelf: SkipSelfFactory = makeParamDecorator(SkipSelfMetadata);

0 commit comments

Comments
 (0)