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

Commit 35b6b0f

Browse files
committed
feat(controller): inherited controllers
1 parent 4dd7aee commit 35b6b0f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/block.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,22 @@ class Block implements ElementWrapper {
166166
new DirectiveValue.fromString(directiveDef.value));
167167

168168
var controllerType = directiveDef.directiveFactory.$controllerType;
169-
var requiredController = directiveDef.directiveFactory.$requiredController;
169+
String requiredController = directiveDef.directiveFactory.$requiredController;
170170

171171
if (requiredController != null) {
172172
directiveModule.factory(Controller, (dom.Node node, Expando elementControllers) {
173+
getInheritedController(n, requiredController) {
174+
if (n == null) return null;
175+
var controller, expando;
176+
if ((expando = elementControllers[n]) != null && (controller = expando[requiredController]) != null) {
177+
return controller;
178+
}
179+
return getInheritedController(n.parentNode, requiredController);
180+
}
181+
if (requiredController.startsWith('\$^')) {
182+
return getInheritedController(node.parentNode, requiredController.replaceFirst('^', ''));
183+
}
184+
if (elementControllers[node] == null) return null;
173185
return elementControllers[node][requiredController];
174186
});
175187
} else {

test/controller_spec.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ class DepAttrDirective {
3838
}
3939
}
4040

41+
class InheritDepAttrDirective {
42+
static var $require = '^[main]';
43+
Log log;
44+
Controller controller;
45+
InheritDepAttrDirective(Log this.log, Controller this.controller) { dump("id"); }
46+
47+
attach(Scope scope) {
48+
log('inheritDep:${controller.name}:${controller.calledFromMainDirective}');
49+
}
50+
}
51+
52+
4153
class OtherAttrDirective {
4254
Log log;
4355
Controller controller;
@@ -62,6 +74,7 @@ main() {
6274
injector.get(Directives)
6375
..register(MainAttrDirective)
6476
..register(DepAttrDirective)
77+
..register(InheritDepAttrDirective)
6578
..register(OtherAttrDirective);
6679

6780
$compile = injector.get(Compiler);
@@ -85,5 +98,23 @@ main() {
8598
expect($log.result()).toEqual('main; dep:main:true; other:false');
8699
});
87100

101+
it('should get a required controller from the parent element', () {
102+
var element = $('<div main><div inherit-dep></div></div>');
103+
var template = $compile(element);
104+
template(element).attach($rootScope);
105+
106+
expect($log.result()).toEqual('main; inheritDep:main:true');
107+
});
108+
109+
it('should get a required controller from an ancestor', () {
110+
var element = $('<div main><div><div inherit-dep></div></div></div>');
111+
var template = $compile(element);
112+
template(element).attach($rootScope);
113+
114+
expect($log.result()).toEqual('main; inheritDep:main:true');
115+
});
116+
117+
xit('should error nicely on missing controller', () {});
118+
88119
});
89120
}

0 commit comments

Comments
 (0)