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

Commit 8cb01fc

Browse files
committed
feat(directives): Custom inject for BlockLists
1 parent 49c9bde commit 8cb01fc

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

lib/block.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ class BlockFactory {
5252
BlockListFactory this.$blockListFactory,
5353
Injector this.$injector);
5454

55-
call(List<dom.Node> blockNodeList, List directivePositions, List<BlockCache> blockCaches, String group) {
55+
call(List<dom.Node> blockNodeList, List directivePositions, List<BlockCache> blockCaches, String group, {injector: null}) {
5656
ASSERT(blockNodeList != null);
5757
ASSERT(directivePositions != null);
5858
ASSERT(blockCaches != null);
59-
return new Block($exceptionHandler, $blockListFactory, $injector,
59+
return new Block($exceptionHandler, $blockListFactory, injector != null ? injector : $injector,
6060
blockNodeList, directivePositions, blockCaches, group);
6161
}
6262
}
@@ -191,6 +191,9 @@ class Block implements ElementWrapper {
191191
new DirectiveValue.fromString(directiveRef.value);
192192
locals[BlockList] = anchorsByName[directiveName];
193193

194+
if (locals[BlockList] != null)
195+
locals[BlockList].customInjector = injector;
196+
194197
Type directiveType = directiveRef.directive.type;
195198

196199
try {

lib/block_list.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class BlockList extends ElementWrapper {
2020
List<dom.Node> elements;
2121
Map<String, BlockType> blockTypes;
2222
BlockCache blockCache;
23+
Injector customInjector;
2324

2425
ElementWrapper previous;
2526
ElementWrapper next;
@@ -49,7 +50,7 @@ class BlockList extends ElementWrapper {
4950
throw new ArgumentError("Unknown block type: '$type'.");
5051
}
5152

52-
block = this.blockTypes[type]();
53+
block = this.blockTypes[type](null, null, customInjector);
5354
}
5455

5556
return block;

lib/block_type.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class BlockType {
2525
ASSERT(group != null);
2626
}
2727

28-
Block call([List<dom.Node> elements, List<BlockCache> blockCaches]) {
29-
if (!?elements) {
28+
Block call([List<dom.Node> elements, List<BlockCache> blockCaches, Injector injector]) {
29+
if (!?elements || elements == null) {
3030
elements = cloneElements(templateElements);
3131
}
3232
if (!?blockCaches || blockCaches == null) {
3333
blockCaches = [];
3434
}
35-
return blockFactory(elements, directivePositions, blockCaches, group);
35+
return blockFactory(elements, directivePositions, blockCaches, group, injector: injector);
3636
}
3737

3838
ClassMirror _getClassMirror(Type type) {

test/compiler_spec.dart

+29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ class LocalAttrDirective {
3737
}
3838
}
3939

40+
class SimpleTranscludeInAttachAttrDirective {
41+
static String $transclude = '.';
42+
static String $visibility = DirectiveVisibility.CHILDREN;
43+
44+
Log log;
45+
BlockList blockList;
46+
47+
SimpleTranscludeInAttachAttrDirective(BlockList this.blockList, Log this.log);
48+
49+
attach(Scope scope) {
50+
var block = blockList.newBlock();
51+
block.insertAfter(blockList);
52+
log('SimpleTransclude');
53+
}
54+
}
55+
56+
class IncludeTranscludeAttrDirective {
57+
IncludeTranscludeAttrDirective(SimpleTranscludeInAttachAttrDirective simple, Log log) {
58+
log('IncludeTransclude');
59+
}
60+
}
4061

4162
main() {
4263

@@ -51,6 +72,8 @@ main() {
5172
..register(NgRepeatAttrDirective)
5273
..register(TabComponent)
5374
..register(PaneComponent)
75+
..register(SimpleTranscludeInAttachAttrDirective)
76+
..register(IncludeTranscludeAttrDirective)
5477
..register(LocalAttrDirective);
5578

5679
$rootScope = injector.get(Scope);
@@ -508,6 +531,12 @@ main() {
508531
expect(log.result()).toEqual('TabComponent-0; LocalAttrDirective-0; PaneComponent-1; LocalAttrDirective-0; PaneComponent-2; LocalAttrDirective-0');
509532
}));
510533

534+
it('should reuse controllers for transclusions', inject((Compiler $compile, Scope $rootScope, Log log) {
535+
var element = $('<div simple-transclude-in-attach include-transclude>block</div>');
536+
$compile(element)(element)..attach($rootScope);
537+
expect(log.result()).toEqual('IncludeTransclude; SimpleTransclude');
538+
}));
539+
511540
it('should throw an exception if required directive is missing', inject((Compiler $compile, Scope $rootScope) {
512541
expect(() {
513542
var element = $('<tab local><pane></pane><pane local></pane></tab>');

0 commit comments

Comments
 (0)