Skip to content

Commit

Permalink
feat(core/directives): process EventEmitter for ''&' bindings or exte…
Browse files Browse the repository at this point in the history
…nd existing function
  • Loading branch information
Hotell committed Apr 20, 2016
1 parent 131b093 commit 9c6170e
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/core/directives/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { DirectiveCtrl, NgmDirective } from '../directive_provider';
import { StringWrapper } from '../../../facade/primitives';
import { ChangeDetectionUtil, SimpleChange } from '../../change_detection/change_detection_util';
import { changesQueueService } from '../../change_detection/changes_queue';
import { EventEmitter } from '../../../facade/async';

const REQUIRE_PREFIX_REGEXP = /^(?:(\^\^?)?(\?)?(\^\^?)?)?/;

Expand Down Expand Up @@ -305,8 +306,11 @@ export function directiveControllerFactory<T extends DirectiveCtrl,U extends Typ
// Finally, invoke the constructor using the injection array and the captured locals
$injector.invoke( controller, instance, StringMapWrapper.assign( locals, $requires ) );

reassignBindingsAndCreteEventEmitters( instance, initialInstanceBindingValues );

// @TODO use this in 2.0
// reassign back the initial binding values, just in case if we used default values
StringMapWrapper.assign( instance, initialInstanceBindingValues );
// StringMapWrapper.assign( instance, initialInstanceBindingValues );


/*if ( isFunction( instance.ngOnDestroy ) ) {
Expand All @@ -321,7 +325,7 @@ export function directiveControllerFactory<T extends DirectiveCtrl,U extends Typ
instance.ngOnChanges( initialChanges );
}

_ddo._ngOnInitBound = function _ngOnInitBound(){
_ddo._ngOnInitBound = function _ngOnInitBound() {

// invoke again only if there are any directive requires
// #perf
Expand All @@ -334,8 +338,11 @@ export function directiveControllerFactory<T extends DirectiveCtrl,U extends Typ

$injector.invoke( controller, instance, StringMapWrapper.assign( locals, $requires ) );

reassignBindingsAndCreteEventEmitters( instance, initialInstanceBindingValues );

// @TODO use this in 2.0
// reassign back the initial binding values, just in case if we used default values
StringMapWrapper.assign( instance, initialInstanceBindingValues );
// StringMapWrapper.assign( instance, initialInstanceBindingValues );
}

if ( isFunction( instance.ngOnInit ) ) {
Expand All @@ -349,6 +356,25 @@ export function directiveControllerFactory<T extends DirectiveCtrl,U extends Typ

}

// @TODO this won't be needed in 2.0 because initial output binding will be wrapped with EventEmitter so just Object.assign will do
function reassignBindingsAndCreteEventEmitters(
instance: {[propName: string]: any},
initialBindings: {[propName: string]: any}
): void {
StringMapWrapper.forEach( initialBindings, ( bindingVal: any, propName: string )=> {

// @TODO in ngMetadata 2.0 we will throw error if instance[propName] will be default assigned function instead of EventEmitter
// check if after constructor instantiation, user assigned directly @Output to new EventEmitter()
if ( instance[propName] instanceof EventEmitter ) {
// this will make sure that instance[ propName ] will be instanceof EventEmitter
instance[ propName ].wrapNgExpBindingToEmitter( initialBindings[ propName ] )
} else {
instance[ propName ] = bindingVal;
}

} );
}

function getInitialBindings( instance ): {[propName: string]: any} {
const initialBindingValues = {};
StringMapWrapper.forEach( instance, ( value: any, propName: string ) => {
Expand Down Expand Up @@ -754,9 +780,19 @@ export function _createDirectiveBindings(
// Don't assign noop to ctrl if expression is not valid
if (parentGet === noop && optional) return;

ctrl[propName] = function(locals) {
return parentGet(scope, locals);
};
// @TODO in ngMetadata 2.0 this will be removed
EventEmitter.makeNgExpBindingEmittable( _exprBindingCb );

// @TODO in ngMetadata 2.0 we will assign this property to EventEmitter directly
// const emitter = new EventEmitter();
// emitter.wrapNgExpBindingToEmitter( _exprBindingCb );
// ctrl[propName] = emitter;

ctrl[propName] = _exprBindingCb;

function _exprBindingCb( locals ) {
return parentGet( scope, locals );
}

}
function _createAttrBinding( attrName: string, propName: string, optional: boolean ): Function {
Expand Down

0 comments on commit 9c6170e

Please sign in to comment.