Skip to content

Commit 3a53f67

Browse files
committed
feat(di): removed publishAs
BREAKING CHANGES Removes the publishAs property from the Component annotation.
1 parent 155b1e2 commit 3a53f67

File tree

4 files changed

+2
-238
lines changed

4 files changed

+2
-238
lines changed

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

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -845,41 +845,6 @@ export class Component extends Directive {
845845
*/
846846
injectables:List;
847847

848-
// TODO(naomib): needs documentation
849-
/**
850-
* Dependency injection tokens that this component publishes _itself_ to its
851-
* children in its view via the application injector.
852-
*
853-
* ## Examples
854-
*
855-
* Imagine you have parent component that implements the [RpcService]
856-
* interface. It can pose as [RpcService] to its children. Child components
857-
* do not need to know about this fact. They only need to declare their
858-
* dependency on [RpcService] without knowing exactly how it is provided.
859-
*
860-
* ```
861-
* @Component({
862-
* selector: 'parent',
863-
* publishAs: [RpcService]
864-
* })
865-
* @View({
866-
* template: '<child></child>',
867-
* directives: [Child]
868-
* })
869-
* class Parent implements RpcService {
870-
* }
871-
*
872-
* @Component({
873-
* selector: 'child'
874-
* })
875-
* class Child {
876-
* // Just asks for RpcService; doesn't know that it's Parent.
877-
* constructor(RpcService rpc);
878-
* }
879-
* ```
880-
*/
881-
publishAs:List;
882-
883848
@CONST()
884849
constructor({
885850
selector,
@@ -892,8 +857,7 @@ export class Component extends Directive {
892857
injectables,
893858
lifecycle,
894859
changeDetection = DEFAULT,
895-
compileChildren = true,
896-
publishAs
860+
compileChildren = true
897861
}:{
898862
selector:string,
899863
properties:Object,
@@ -905,8 +869,7 @@ export class Component extends Directive {
905869
injectables:List,
906870
lifecycle:List,
907871
changeDetection:string,
908-
compileChildren:boolean,
909-
publishAs:List
872+
compileChildren:boolean
910873
}={})
911874
{
912875
super({
@@ -923,7 +886,6 @@ export class Component extends Directive {
923886

924887
this.changeDetection = changeDetection;
925888
this.injectables = injectables;
926-
this.publishAs = publishAs;
927889
}
928890
}
929891

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,12 @@ export class DirectiveDependency extends Dependency {
227227
export class DirectiveBinding extends ResolvedBinding {
228228
resolvedInjectables:List<ResolvedBinding>;
229229
metadata: DirectiveMetadata;
230-
publishAs: List<Type>;
231230

232231
constructor(key:Key, factory:Function, dependencies:List, providedAsPromise:boolean,
233232
resolvedInjectables:List<ResolvedBinding>, metadata:DirectiveMetadata, annotation: Directive) {
234233
super(key, factory, dependencies, providedAsPromise);
235234
this.resolvedInjectables = resolvedInjectables;
236235
this.metadata = metadata;
237-
if (annotation instanceof Component) {
238-
this.publishAs = annotation.publishAs;
239-
} else {
240-
this.publishAs = null;
241-
}
242236
}
243237

244238
get callOnDestroy() {
@@ -678,20 +672,6 @@ export class ElementInjector extends TreeNode {
678672

679673
var p = this._proto;
680674
if (isPresent(p._keyId0)) this._getDirectiveByKeyId(p._keyId0);
681-
if (isPresent(shadowDomAppInjector)) {
682-
var publishAs = this._proto._binding0.publishAs;
683-
if (isPresent(publishAs) && publishAs.length > 0) {
684-
// If there's a component directive on this element injector, then
685-
// 0-th key must contain the directive itself.
686-
// TODO(yjbanov): need to make injector creation faster:
687-
// - remove flattening of bindings array
688-
// - precalc token key
689-
this._shadowDomAppInjector = shadowDomAppInjector.resolveAndCreateChild(
690-
ListWrapper.map(publishAs, (token) => {
691-
return bind(token).toValue(this.getComponent());
692-
}));
693-
}
694-
}
695675
if (isPresent(p._keyId1)) this._getDirectiveByKeyId(p._keyId1);
696676
if (isPresent(p._keyId2)) this._getDirectiveByKeyId(p._keyId2);
697677
if (isPresent(p._keyId3)) this._getDirectiveByKeyId(p._keyId3);

modules/angular2/test/core/compiler/element_injector_spec.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,6 @@ class TestNode extends TreeNode {
195195
}
196196
}
197197

198-
// TypeScript erases interfaces, so it has to be a class
199-
class ParentInterface {}
200-
201-
class ParentComponent extends ParentInterface {
202-
}
203-
204-
class AppDependency {
205-
parent:ParentInterface;
206-
207-
constructor(p:ParentInterface) {
208-
this.parent = p;
209-
}
210-
}
211-
212-
class ChildComponent {
213-
parent:ParentInterface;
214-
appDependency:AppDependency;
215-
216-
constructor(p:ParentInterface, a:AppDependency) {
217-
this.parent = p;
218-
this.appDependency = a;
219-
}
220-
}
221-
222198
export function main() {
223199
var defaultPreBuiltObjects = new PreBuiltObjects(null, null, null);
224200
var appInjector = Injector.resolveAndCreate([]);
@@ -647,31 +623,6 @@ export function main() {
647623
inj.clearDirectives();
648624
expect(destroy.onDestroyCounter).toBe(1);
649625
});
650-
651-
it("should publish component to its children via app injector when requested", function() {
652-
var parentDirective = new Component({
653-
selector: 'parent',
654-
publishAs: [ParentInterface]
655-
});
656-
var parentBinding = DirectiveBinding.createFromType(ParentComponent, parentDirective);
657-
658-
var childDirective = new Component({
659-
selector: 'child',
660-
injectables: [AppDependency]
661-
});
662-
var childBinding = DirectiveBinding.createFromType(ChildComponent, childDirective);
663-
664-
var child = hostShadowInjectors([parentBinding], [childBinding], true, true);
665-
var d = child.get(ChildComponent);
666-
667-
// Verify that the child component can inject parent via interface binding
668-
expect(d).toBeAnInstanceOf(ChildComponent);
669-
expect(d.parent).toBeAnInstanceOf(ParentComponent);
670-
671-
// Verify that the binding is available down the dependency tree
672-
expect(d.appDependency.parent).toBeAnInstanceOf(ParentComponent);
673-
expect(d.parent).toBe(d.appDependency.parent);
674-
});
675626
});
676627

677628
describe("dynamicallyCreateComponent", () => {

modules/angular2/test/core/compiler/integration_spec.js

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -986,71 +986,6 @@ export function main() {
986986
}));
987987
}
988988
});
989-
990-
describe('dependency injection', () => {
991-
992-
it('should publish parent component to shadow DOM via publishAs',
993-
inject([TestBed, AsyncTestCompleter, Compiler], (tb, async, compiler) => {
994-
tb.overrideView(MyComp, new View({
995-
template: `<parent></parent>`,
996-
directives: [ParentComponent]
997-
}));
998-
999-
tb.createView(MyComp).then((view) => {
1000-
view.detectChanges();
1001-
expect(view.rootNodes).toHaveText(
1002-
'Parent,Parent');
1003-
async.done();
1004-
});
1005-
}));
1006-
1007-
it('should override parent bindings via publishAs',
1008-
inject([TestBed, AsyncTestCompleter, Compiler], (tb, async, compiler) => {
1009-
tb.overrideView(MyComp, new View({
1010-
template: `<recursive-parent></recursive-parent>`,
1011-
directives: [RecursiveParentComponent]
1012-
}));
1013-
1014-
tb.createView(MyComp).then((view) => {
1015-
view.detectChanges();
1016-
expect(view.rootNodes).toHaveText(
1017-
'ParentInterface,RecursiveParent,RecursiveParent');
1018-
async.done();
1019-
});
1020-
}));
1021-
1022-
// [DynamicComponentLoader] already supports providing a custom
1023-
// injector as an argument to `loadIntoExistingLocation`, which should
1024-
// be used instead of `publishAs`.
1025-
//
1026-
// Conceptually dynamically loaded components are loaded _instead_ of
1027-
// the dynamic component itself. The dynamic component does not own the
1028-
// shadow DOM. It's the loaded component that creates that shadow DOM.
1029-
it('should not publish into dynamically instantiated components via publishAs',
1030-
inject([TestBed, AsyncTestCompleter, Compiler], (tb, async, compiler) => {
1031-
tb.overrideView(MyComp, new View({
1032-
template: `<dynamic-parent #cmp></dynamic-parent>`,
1033-
directives: [DynamicParentComponent]
1034-
}));
1035-
1036-
tb.createView(MyComp).then((view) => {
1037-
view.detectChanges();
1038-
var comp = view.rawView.locals.get("cmp");
1039-
PromiseWrapper.then(comp.done,
1040-
(value) => {
1041-
throw new BaseException(`Expected to throw error, but got value ${value}`);
1042-
},
1043-
(err) => {
1044-
expect(err.message)
1045-
.toEqual('No provider for ParentInterface! (ChildComponent -> ParentInterface)');
1046-
async.done();
1047-
}
1048-
);
1049-
});
1050-
}));
1051-
1052-
});
1053-
1054989
});
1055990
}
1056991

@@ -1479,70 +1414,6 @@ class NeedsPublicApi {
14791414
}
14801415
}
14811416

1482-
class ParentInterface {
1483-
message:String;
1484-
constructor() {
1485-
this.message = 'ParentInterface';
1486-
}
1487-
}
1488-
1489-
@Component({
1490-
selector: 'parent',
1491-
publishAs: [ParentInterface]
1492-
})
1493-
@View({
1494-
template: `<child></child>`,
1495-
directives: [ChildComponent]
1496-
})
1497-
class ParentComponent extends ParentInterface {
1498-
message:String;
1499-
constructor() {
1500-
super();
1501-
this.message = 'Parent';
1502-
}
1503-
}
1504-
1505-
@Component({
1506-
injectables: [ParentInterface],
1507-
selector: 'recursive-parent',
1508-
publishAs: [ParentInterface]
1509-
})
1510-
@View({
1511-
template: `{{parentService.message}},<child></child>`,
1512-
directives: [ChildComponent]
1513-
})
1514-
class RecursiveParentComponent extends ParentInterface {
1515-
parentService:ParentInterface;
1516-
message:String;
1517-
constructor(parentService:ParentInterface) {
1518-
super();
1519-
this.message = 'RecursiveParent';
1520-
this.parentService = parentService;
1521-
}
1522-
}
1523-
1524-
@Component({
1525-
selector: 'dynamic-parent',
1526-
publishAs: [ParentInterface]
1527-
})
1528-
class DynamicParentComponent extends ParentInterface {
1529-
message:String;
1530-
done;
1531-
constructor(loader:DynamicComponentLoader, location:ElementRef) {
1532-
super();
1533-
this.message = 'DynamicParent';
1534-
this.done = loader.loadIntoExistingLocation(ChildComponent, location);
1535-
}
1536-
}
1537-
1538-
class AppDependency {
1539-
parent:ParentInterface;
1540-
1541-
constructor(p:ParentInterface) {
1542-
this.parent = p;
1543-
}
1544-
}
1545-
15461417
@Component({
15471418
selector: 'child',
15481419
injectables: [AppDependency]

0 commit comments

Comments
 (0)