Skip to content

Commit e3b7724

Browse files
committed
feat(ElementInjector): change ElementInjector to accept bindings or types
1 parent 94958e0 commit e3b7724

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

modules/core/src/compiler/element_injector.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class ProtoElementInjector extends TreeNode {
145145
@FIELD('_key8:int')
146146
@FIELD('_key9:int')
147147
@FIELD('textNodes:List<int>')
148-
constructor(parent:ProtoElementInjector, directiveTypes:List, textNodes:List) {
148+
constructor(parent:ProtoElementInjector, bindings:List, textNodes:List) {
149149
super(parent);
150150

151151
this._elementInjector = null;
@@ -161,18 +161,18 @@ export class ProtoElementInjector extends TreeNode {
161161
this._binding8 = null; this._keyId8 = null;
162162
this._binding9 = null; this._keyId9 = null;
163163

164-
var length = directiveTypes.length;
165-
166-
if (length > 0) {this._binding0 = this._createBinding(directiveTypes[0]); this._keyId0 = this._binding0.key.id;}
167-
if (length > 1) {this._binding1 = this._createBinding(directiveTypes[1]); this._keyId1 = this._binding1.key.id;}
168-
if (length > 2) {this._binding2 = this._createBinding(directiveTypes[2]); this._keyId2 = this._binding2.key.id;}
169-
if (length > 3) {this._binding3 = this._createBinding(directiveTypes[3]); this._keyId3 = this._binding3.key.id;}
170-
if (length > 4) {this._binding4 = this._createBinding(directiveTypes[4]); this._keyId4 = this._binding4.key.id;}
171-
if (length > 5) {this._binding5 = this._createBinding(directiveTypes[5]); this._keyId5 = this._binding5.key.id;}
172-
if (length > 6) {this._binding6 = this._createBinding(directiveTypes[6]); this._keyId6 = this._binding6.key.id;}
173-
if (length > 7) {this._binding7 = this._createBinding(directiveTypes[7]); this._keyId7 = this._binding7.key.id;}
174-
if (length > 8) {this._binding8 = this._createBinding(directiveTypes[8]); this._keyId8 = this._binding8.key.id;}
175-
if (length > 9) {this._binding9 = this._createBinding(directiveTypes[9]); this._keyId9 = this._binding9.key.id;}
164+
var length = bindings.length;
165+
166+
if (length > 0) {this._binding0 = this._createBinding(bindings[0]); this._keyId0 = this._binding0.key.id;}
167+
if (length > 1) {this._binding1 = this._createBinding(bindings[1]); this._keyId1 = this._binding1.key.id;}
168+
if (length > 2) {this._binding2 = this._createBinding(bindings[2]); this._keyId2 = this._binding2.key.id;}
169+
if (length > 3) {this._binding3 = this._createBinding(bindings[3]); this._keyId3 = this._binding3.key.id;}
170+
if (length > 4) {this._binding4 = this._createBinding(bindings[4]); this._keyId4 = this._binding4.key.id;}
171+
if (length > 5) {this._binding5 = this._createBinding(bindings[5]); this._keyId5 = this._binding5.key.id;}
172+
if (length > 6) {this._binding6 = this._createBinding(bindings[6]); this._keyId6 = this._binding6.key.id;}
173+
if (length > 7) {this._binding7 = this._createBinding(bindings[7]); this._keyId7 = this._binding7.key.id;}
174+
if (length > 8) {this._binding8 = this._createBinding(bindings[8]); this._keyId8 = this._binding8.key.id;}
175+
if (length > 9) {this._binding9 = this._createBinding(bindings[9]); this._keyId9 = this._binding9.key.id;}
176176
if (length > 10) {
177177
throw 'Maximum number of directives per element has been reached.';
178178
}
@@ -194,8 +194,10 @@ export class ProtoElementInjector extends TreeNode {
194194
return this._elementInjector;
195195
}
196196

197-
_createBinding(directiveType:Type) {
198-
var b = bind(directiveType).toClass(directiveType);
197+
_createBinding(bindingOrType) {
198+
var b = (bindingOrType instanceof Type) ?
199+
bind(bindingOrType).toClass(bindingOrType) :
200+
bindingOrType;
199201
var deps = ListWrapper.map(b.dependencies, DirectiveDependency.createFrom);
200202
return new Binding(b.key, b.factory, deps, b.providedAsPromise);
201203
}

modules/core/test/compiler/element_injector_spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ export function main() {
191191
expect(() => injector([NeedDirectiveFromParent])).
192192
toThrowError('No provider for Directive! (NeedDirectiveFromParent -> Directive)');
193193
});
194+
195+
it("should accept bindings instead of directive types", function () {
196+
var inj = injector([bind(Directive).toClass(Directive)]);
197+
expect(inj.get(Directive)).toBeAnInstanceOf(Directive);
198+
});
194199
});
195200

196201
describe("special objects", function () {

0 commit comments

Comments
 (0)