fix(DirectiveMetadata): add support for events, changeDetection

This commit is contained in:
Victor Berchet 2015-06-12 09:50:30 +02:00
parent db3d5d4941
commit b4e82b8bc7
7 changed files with 109 additions and 10 deletions

View File

@ -24,6 +24,8 @@ export function directiveMetadataToMap(meta: DirectiveMetadata): Map<string, any
['callOnInit', meta.callOnInit],
['callOnChange', meta.callOnChange],
['callOnAllChangesDone', meta.callOnAllChangesDone],
['events', meta.events],
['changeDetection', meta.changeDetection],
['version', 1],
]);
}
@ -50,7 +52,9 @@ export function directiveMetadataFromMap(map: Map<string, any>): DirectiveMetada
callOnCheck:<boolean>MapWrapper.get(map, 'callOnCheck'),
callOnChange:<boolean>MapWrapper.get(map, 'callOnChange'),
callOnInit:<boolean>MapWrapper.get(map, 'callOnInit'),
callOnAllChangesDone:<boolean>MapWrapper.get(map, 'callOnAllChangesDone')
callOnAllChangesDone:<boolean>MapWrapper.get(map, 'callOnAllChangesDone'),
events:<List<string>>_cloneIfPresent(MapWrapper.get(map, 'events')),
changeDetection:<string>MapWrapper.get(map, 'changeDetection'),
});
}

View File

@ -63,8 +63,8 @@ class _DirectiveMetadataVisitor extends Object
bool _callOnCheck;
bool _callOnInit;
bool _callOnAllChangesDone;
String changeDetection;
List<String> events;
String _changeDetection;
List<String> _events;
final ConstantEvaluator _evaluator = new ConstantEvaluator();
@ -83,8 +83,8 @@ class _DirectiveMetadataVisitor extends Object
_callOnCheck = false;
_callOnInit = false;
_callOnAllChangesDone = false;
changeDetection = null;
events = [];
_changeDetection = null;
_events = [];
}
DirectiveMetadata get meta => DirectiveMetadata.create(
@ -100,8 +100,8 @@ class _DirectiveMetadataVisitor extends Object
callOnCheck: _callOnCheck,
callOnInit: _callOnInit,
callOnAllChangesDone: _callOnAllChangesDone,
changeDetection: changeDetection,
events: events);
changeDetection: _changeDetection,
events: _events);
@override
Object visitAnnotation(Annotation node) {
@ -145,7 +145,6 @@ class _DirectiveMetadataVisitor extends Object
'$node' /* source */);
}
var keyString = '${node.name.label}';
// TODO(kegluneq): Populate the other values in [DirectiveMetadata]
switch (keyString) {
case 'selector':
_populateSelector(node.expression);
@ -165,6 +164,12 @@ class _DirectiveMetadataVisitor extends Object
case 'exportAs':
_populateExportAs(node.expression);
break;
case 'changeDetection':
_populateChangeDetection(node.expression);
break;
case 'events':
_populateEvents(node.expression);
break;
}
return null;
}
@ -263,4 +268,14 @@ class _DirectiveMetadataVisitor extends Object
_callOnInit = lifecycleEvents.contains("onInit");
_callOnAllChangesDone = lifecycleEvents.contains("onAllChangesDone");
}
void _populateEvents(Expression eventsValue) {
_checkMeta();
_populateList(eventsValue, _events, 'Directive#events');
}
void _populateChangeDetection(Expression value) {
_checkMeta();
_changeDetection = _expressionToString(value, 'Directive#changeDetection');
}
}

View File

@ -22,7 +22,9 @@ export function main() {
callOnChange: true,
callOnCheck: true,
callOnInit: true,
callOnAllChangesDone: true
callOnAllChangesDone: true,
events: ['onFoo', 'onBar'],
changeDetection: 'CHECK_ONCE'
});
var map = directiveMetadataToMap(someComponent);
expect(MapWrapper.get(map, 'compileChildren')).toEqual(false);
@ -45,6 +47,8 @@ export function main() {
expect(MapWrapper.get(map, 'callOnInit')).toEqual(true);
expect(MapWrapper.get(map, 'callOnAllChangesDone')).toEqual(true);
expect(MapWrapper.get(map, 'exportAs')).toEqual('aaa');
expect(MapWrapper.get(map, 'events')).toEqual(['onFoo', 'onBar']);
expect(MapWrapper.get(map, 'changeDetection')).toEqual('CHECK_ONCE');
});
it('mapToDirectiveMetadata', () => {
@ -64,7 +68,9 @@ export function main() {
['callOnCheck', true],
['callOnInit', true],
['callOnChange', true],
['callOnAllChangesDone', true]
['callOnAllChangesDone', true],
['events', ['onFoo', 'onBar']],
['changeDetection', 'CHECK_ONCE']
]);
var meta = directiveMetadataFromMap(map);
expect(meta.compileChildren).toEqual(false);
@ -83,6 +89,8 @@ export function main() {
expect(meta.callOnInit).toEqual(true);
expect(meta.callOnChange).toEqual(true);
expect(meta.callOnAllChangesDone).toEqual(true);
expect(meta.events).toEqual(['onFoo', 'onBar']);
expect(meta.changeDetection).toEqual('CHECK_ONCE');
});
});
}

View File

@ -109,6 +109,18 @@ void allTests() {
expect(metadata.callOnAllChangesDone).toBe(true);
});
it('should parse events.', () async {
var metadata = await readMetadata('directive_metadata_extractor/'
'directive_metadata_files/events.ng_deps.dart');
expect(metadata.events).toEqual(['onFoo', 'onBar']);
});
it('should parse changeDetection.', () async {
var metadata = await readMetadata('directive_metadata_extractor/'
'directive_metadata_files/changeDetection.ng_deps.dart');
expect(metadata.changeDetection).toEqual('CHECK_ONCE');
});
it('should fail when a class is annotated with multiple Directives.',
() async {
var ngDeps = await NgDeps.parse(reader, new AssetId('a',

View File

@ -0,0 +1,29 @@
library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
bootstrap,
Component,
Directive,
View,
NgElement,
onChange,
onDestroy,
onInit,
onCheck,
onAllChangesDone;
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(HelloCmp, {
'factory': () => new HelloCmp(),
'parameters': const [const []],
'annotations': const [
const Component(changeDetection: 'CHECK_ONCE')
]
});
}

View File

@ -0,0 +1,29 @@
library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
bootstrap,
Component,
Directive,
View,
NgElement,
onChange,
onDestroy,
onInit,
onCheck,
onAllChangesDone;
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(HelloCmp, {
'factory': () => new HelloCmp(),
'parameters': const [const []],
'annotations': const [
const Component(events: ['onFoo', 'onBar'])
]
});
}

View File

@ -16,6 +16,8 @@
"callOnInit": false,
"callOnChange": false,
"callOnAllChangesDone": false,
"events": [],
"changeDetection": null,
"version": 1
}
}