feat: alllow specifying directives as bindings

Related to #709
Closes #1498
This commit is contained in:
Pawel Kozlowski 2015-04-22 10:46:09 +02:00 committed by Tobias Bosch
parent 6896305e34
commit 4bab25b366
2 changed files with 53 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import {Injectable} from 'angular2/di';
import {Injectable, Binding} from 'angular2/di';
import {Type, isBlank, isPresent, BaseException, normalizeBlank, stringify} from 'angular2/src/facade/lang';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
@ -76,9 +76,13 @@ export class Compiler {
_bindDirective(directiveTypeOrBinding):DirectiveBinding {
if (directiveTypeOrBinding instanceof DirectiveBinding) {
return directiveTypeOrBinding;
} else if (directiveTypeOrBinding instanceof Binding) {
let meta = this._reader.read(directiveTypeOrBinding.token);
return DirectiveBinding.createFromBinding(directiveTypeOrBinding, meta.annotation);
} else {
let meta = this._reader.read(directiveTypeOrBinding);
return DirectiveBinding.createFromType(meta.type, meta.annotation);
}
var meta = this._reader.read(directiveTypeOrBinding);
return DirectiveBinding.createFromType(meta.type, meta.annotation);
}
// Create a hostView as if the compiler encountered <hostcmp></hostcmp>.

View File

@ -274,6 +274,31 @@ export function main() {
});
}));
it('should allow specifying directives as bindings', inject([TestBed, AsyncTestCompleter], (tb, async) => {
tb.overrideView(MyComp, new View({
template: '<child-cmp></child-cmp>',
directives: [bind(ChildComp).toClass(ChildComp)]
}));
tb.createView(MyComp, {context: ctx}).then((view) => {
view.detectChanges();
expect(view.rootNodes).toHaveText('hello');
async.done();
});
}));
it('should read directives metadata from their binding token', inject([TestBed, AsyncTestCompleter], (tb, async) => {
tb.overrideView(MyComp, new View({
template: '<div public-api><div needs-public-api></div></div>',
directives: [bind(PublicApi).toClass(PrivateImpl), NeedsPublicApi]
}));
tb.createView(MyComp, {context: ctx}).then((view) => {
async.done();
});
}));
it('should support template directives via `<template>` elements.', inject([TestBed, AsyncTestCompleter], (tb, async) => {
tb.overrideView(MyComp,
new View({
@ -1191,3 +1216,24 @@ class NeedsAttribute {
this.fooAttribute = fooAttribute;
}
}
@Decorator({
selector: '[public-api]'
})
class PublicApi {
}
@Decorator({
selector: '[private-impl]'
})
class PrivateImpl extends PublicApi {
}
@Decorator({
selector: '[needs-public-api]'
})
class NeedsPublicApi {
constructor(@Parent() api:PublicApi) {
expect(api instanceof PrivateImpl).toBe(true);
}
}