fix(di): allow injecting event emitter fns without specifying type annotation

Fixes #965

Closes #1155
This commit is contained in:
Pawel Kozlowski 2015-03-29 14:56:18 +02:00
parent 9adf41ca2d
commit ae30d7ba40
4 changed files with 71 additions and 5 deletions

View File

@ -12,6 +12,10 @@ export class EventEmitter extends DependencyAnnotation {
super();
this.eventName = eventName;
}
get token() {
return Function;
}
}
/**
@ -25,6 +29,10 @@ export class PropertySetter extends DependencyAnnotation {
super();
this.propName = propName;
}
get token() {
return Function;
}
}
/**

View File

@ -107,6 +107,10 @@ export class DependencyAnnotation {
@CONST()
constructor() {
}
get token() {
return null;
}
}
/**

View File

@ -135,7 +135,11 @@ function _extractToken(typeOrFunc, annotations) {
optional = true;
} else if (paramAnnotation instanceof DependencyAnnotation) {
if (isPresent(paramAnnotation.token)) {
token = paramAnnotation.token;
}
ListWrapper.push(depProps, paramAnnotation);
} else if (paramAnnotation.name === "string") {
token = paramAnnotation;
}

View File

@ -79,6 +79,16 @@ class NeedsEventEmitter {
}
}
class NeedsEventEmitterNoType {
clickEmitter;
constructor(@EventEmitter('click') clickEmitter) {
this.clickEmitter = clickEmitter;
}
click() {
this.clickEmitter(null);
}
}
class NeedsPropertySetter {
propSetter;
roleSetter;
@ -113,6 +123,17 @@ class NeedsPropertySetter {
}
}
class NeedsPropertySetterNoType {
propSetter;
constructor(@PropertySetter('title') propSetter) {
this.propSetter = propSetter;
}
setProp(value) {
this.propSetter(value);
}
}
class NeedsAttribute {
typeAttribute;
titleAttribute;
@ -545,24 +566,43 @@ export function main() {
});
describe('event emitters', () => {
it('should be injectable and callable', () => {
var called = false;
function createpreBuildObject(eventName, eventHandler) {
var handlers = StringMapWrapper.create();
StringMapWrapper.set(handlers, 'click', (e, view) => { called = true;});
StringMapWrapper.set(handlers, eventName, eventHandler);
var pv = new ProtoView(null, null, null);
pv.eventHandlers = [handlers];
var view = new View(pv, null, MapWrapper.create());
var preBuildObject = new PreBuiltObjects(view, null, null, null);
return new PreBuiltObjects(view, null, null, null);
}
it('should be injectable and callable', () => {
var called = false;
var preBuildObject = createpreBuildObject('click', (e, view) => { called = true;});
var inj = injector([NeedsEventEmitter], null, null, preBuildObject);
inj.get(NeedsEventEmitter).click();
expect(called).toEqual(true);
});
it('should be injectable and callable without specifying param type annotation', () => {
var called = false;
var preBuildObject = createpreBuildObject('click', (e, view) => { called = true;});
var inj = injector([NeedsEventEmitterNoType], null, null, preBuildObject);
inj.get(NeedsEventEmitterNoType).click();
expect(called).toEqual(true);
});
it('should be queryable through hasEventEmitter', () => {
var inj = injector([NeedsEventEmitter]);
expect(inj.hasEventEmitter('click')).toBe(true);
expect(inj.hasEventEmitter('move')).toBe(false);
});
it('should be queryable through hasEventEmitter without specifying param type annotation', () => {
var inj = injector([NeedsEventEmitterNoType]);
expect(inj.hasEventEmitter('click')).toBe(true);
expect(inj.hasEventEmitter('move')).toBe(false);
});
});
describe('property setter', () => {
@ -577,7 +617,7 @@ export function main() {
component.setRole('button');
component.setClass(true);
component.classWithDashSetter(true);
component.setStyle('40px')
component.setStyle('40px');
component.setStyleWithUnit(50);
expect(div.title).toEqual('foobar');
@ -587,6 +627,16 @@ export function main() {
expect(DOM.getStyle(div, 'width')).toEqual('40px');
expect(DOM.getStyle(div, 'height')).toEqual('50px');
});
it('should be injectable and callable without specifying param type annotation', () => {
var div = el('<div></div>');
var preBuildObject = new PreBuiltObjects(null, new NgElement(div), null, null);
var inj = injector([NeedsPropertySetterNoType], null, null, preBuildObject);
var component = inj.get(NeedsPropertySetterNoType);
component.setProp('foobar');
expect(div.title).toEqual('foobar');
});
});
describe('static', () => {