X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion modules/angular2/src/core/annotations_impl/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ export class Directive extends Injectable {
* events: ['statusChange']
* })
* class TaskComponent {
* statusChange:EventEmitter;
* statusChange: EventEmitter;
*
* constructor() {
* this.statusChange = new EventEmitter();
Expand All @@ -561,6 +561,26 @@ export class Directive extends Injectable {
* }
* }
* ```
*
* Use `propertyName: eventName` when the event emitter property name is different from the name
* of the emitted event:
*
* @Component({
* events: ['status: statusChange']
* })
* class TaskComponent {
* status: EventEmitter;
*
* constructor() {
* this.status = new EventEmitter();
* }
*
* onComplete() {
* this.status.next('completed');
* }
* }
* ```
*
*/
events: List<string>;

Expand Down
19 changes: 16 additions & 3 deletions modules/angular2/src/core/compiler/element_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Type,
BaseException,
stringify,
CONST_EXPR
CONST_EXPR,
StringWrapper
} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
Expand Down Expand Up @@ -382,8 +383,20 @@ export class BindingData {
createEventEmitterAccessors() {
if (!(this.binding instanceof DirectiveBinding)) return [];
var db = <DirectiveBinding>this.binding;
return ListWrapper.map(db.eventEmitters, eventName => new EventEmitterAccessor(
eventName, reflector.getter(eventName)));
return ListWrapper.map(db.eventEmitters, eventConfig => {
let fieldName;
let eventName;
var colonIdx = eventConfig.indexOf(':');
if (colonIdx > -1) {
// long format: 'fieldName: eventName'
fieldName = StringWrapper.substring(eventConfig, 0, colonIdx).trim();
eventName = StringWrapper.substring(eventConfig, colonIdx + 1).trim();
} else {
// short format: 'name' when fieldName and eventName are the same
fieldName = eventName = eventConfig;
}
return new EventEmitterAccessor(eventName, reflector.getter(fieldName))
});
}

createHostActionAccessors() {
Expand Down
12 changes: 12 additions & 0 deletions modules/angular2/test/core/compiler/element_injector_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ export function main() {
expect(accessor.getter(new HasEventEmitter())).toEqual('emitter');
});

it('should allow a different internal vs public name', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventName and fieldName?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I'll update and merge

var binding = DirectiveBinding.createFromType(HasEventEmitter,
new dirAnn.Directive({events: ['emitter: publicEmitter']}));

var inj = createPei(null, 0, [binding]);
expect(inj.eventEmitterAccessors.length).toEqual(1);

var accessor = inj.eventEmitterAccessors[0][0];
expect(accessor.eventName).toEqual('publicEmitter');
expect(accessor.getter(new HasEventEmitter())).toEqual('emitter');
});

it('should return a list of hostAction accessors', () => {
var binding = DirectiveBinding.createFromType(
HasEventEmitter, new dirAnn.Directive({hostActions: {'hostActionName': 'onAction'}}));
Expand Down
X Tutup