Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 2fe31d2

Browse files
committed
feat(compiler): support $publishAs on components.
1 parent e5c0142 commit 2fe31d2

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

lib/block.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ class ComponentFactory {
282282
getString(directive.$templateUrl).
283283
then((data) => shadowScope.$apply(() => compileTemplate(data)));
284284
}
285+
if (directive.$publishAs != null) {
286+
shadowScope[directive.$publishAs] = controller;
287+
}
285288
return controller;
286289
}
287290

lib/directive.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Directive {
1414
String $template;
1515
String $templateUrl;
1616
String $cssUrl;
17+
String $publishAs;
1718
Map<String, String> $map;
1819
String $visibility;
1920
ShadowRootOptions $shadowRootOptions;
@@ -51,6 +52,7 @@ class Directive {
5152
$visibility = _defaultIfNull(
5253
reflectStaticField(type, '\$visibility'), DirectiveVisibility.LOCAL);
5354
$map = reflectStaticField(type, '\$map');
55+
$publishAs = reflectStaticField(type, r'$publishAs');
5456
isStructural = $transclude != null;
5557
var $selector = reflectStaticField(type, r'$selector');
5658
if ($selector != null) {

test/compiler_spec.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ main() {
392392
beforeEach(module((AngularModule module) {
393393
module.directive(SimpleComponent);
394394
module.directive(IoComponent);
395+
module.directive(PublishMeComponent);
395396
}));
396397

397398
it('should create a simple component', inject(() {
@@ -422,10 +423,19 @@ main() {
422423
expect($rootScope.done).toEqual(true);
423424
}));
424425

425-
it('should allow the component to publish itself into the scope', inject(() {
426-
426+
it('should throw an exception if required directive is missing', inject((Compiler $compile, Scope $rootScope, Injector injector) {
427+
expect(() {
428+
var element = $('<tab local><pane></pane><pane local></pane></tab>');
429+
$compile(element)(injector, element);
430+
}, throwsA(contains('No provider found for LocalAttrDirective! (resolving LocalAttrDirective)')));
427431
}));
428432

433+
iit('should publish component controller into the scope', inject(() {
434+
var element = $(r'<div><publish-me></publish-me></div>');
435+
$compile(element)(injector, element);
436+
$rootScope.$apply();
437+
expect(element.textWithShadow()).toEqual('WORKED');
438+
}));
429439
});
430440

431441
describe('controller scoping', () {
@@ -443,13 +453,6 @@ main() {
443453
expect(log.result()).toEqual('IncludeTransclude; SimpleTransclude');
444454
}));
445455

446-
it('should throw an exception if required directive is missing', inject((Compiler $compile, Scope $rootScope, Injector injector) {
447-
expect(() {
448-
var element = $('<tab local><pane></pane><pane local></pane></tab>');
449-
$compile(element)(injector, element);
450-
}, throwsA(contains('No provider found for LocalAttrDirective! (resolving LocalAttrDirective)')));
451-
}));
452-
453456
});
454457
});
455458
}
@@ -470,3 +473,11 @@ class IoComponent {
470473
scope.$root.ioComponent = this;
471474
}
472475
}
476+
477+
class PublishMeComponent {
478+
static String $template = r'<content>{{ctrlName.value}}</content>';
479+
static String $publishAs = 'ctrlName';
480+
481+
String value = 'WORKED';
482+
PublishMeComponent() {}
483+
}

0 commit comments

Comments
 (0)