Skip to content

Commit

Permalink
fix(core/directive_provider): fix initial @Input/@attr binding assign…
Browse files Browse the repository at this point in the history
… for @directive

- we have been relying on $watch or @observe but those would be called after lifecycle hooks which
is not how @component resolves things
  • Loading branch information
Hotell committed Mar 2, 2016
1 parent f6c2a33 commit 193834b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/core/directives/directive_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,11 @@ export function _createDirectiveBindings(

// setup @Inputs
StringMapWrapper.forEach( _extractBindings( inputs ), ( alias: string, propName: string )=> {

// set those values as component bindToController does
ctrl[ propName ] = scope.$evalAsync( attributes[ alias || propName ] );

// register watchers for further changes
_internalWatchers.push(
scope.$watch(
attributes[ alias || propName ],
Expand All @@ -741,13 +746,26 @@ export function _createDirectiveBindings(
}
)
)

} );

// setup @Outputs
StringMapWrapper.forEach( _extractBindings( outputs ), ( alias: string, propName: string )=> {

// set those values as component bindToController does
ctrl[ propName ] = ()=> scope.$evalAsync( attributes[ alias || propName ] );

} );

// setup @Attrs
StringMapWrapper.forEach( _extractBindings( attrs ), ( alias: string, propName: string )=> {

// set those values as component bindToController does
ctrl[ propName ] = attributes[ alias || propName ];

// register watchers for further changes
// The observer function will be invoked once during the next $digest following compilation.
// The observer is then invoked whenever the interpolated value changes.
_internalObservers.push(
attributes.$observe(
alias || propName,
Expand All @@ -756,6 +774,7 @@ export function _createDirectiveBindings(
}
)
)

} );

return {
Expand Down
19 changes: 14 additions & 5 deletions test/core/directives/directive_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,14 @@ describe( `directives/directive_provider`, ()=> {
const [[firstExp,firstList],[secondExp,secondList]] = $scope.$$watchers;

expect(firstExp).to.equal(undefined);
expect( ctrl.foo ).to.equal( undefined );
expect( ctrl.foo ).to.equal( ' evaluated' );

firstList( 'hello' );
expect( ctrl.foo ).to.equal( 'hello' );

expect(secondExp).to.equal(undefined);
expect( ctrl.one ).to.equal( undefined );
expect( ctrl.one ).to.equal( ' evaluated' );

secondList( 'hello one' );
expect( ctrl.one ).to.equal( 'hello one' );

Expand All @@ -1043,6 +1045,11 @@ describe( `directives/directive_provider`, ()=> {
'one: oneAlias'
]
} as DirectiveMetadata;


$attrs.foo = 'hello first';
$attrs.oneAlias = 'hello one';

const bindingDisposables = _createDirectiveBindings( $scope, $attrs, ctrl, metadata );
const {watchers,observers} = bindingDisposables;

Expand All @@ -1052,15 +1059,17 @@ describe( `directives/directive_provider`, ()=> {
const [[firstExp,firstList],[secondExp,secondList]] = $attrs.$$observers;

expect(firstExp).to.equal('foo');
expect( ctrl.foo ).to.equal( undefined );
expect( ctrl.foo ).to.equal( 'hello first' );

firstList( 'hello' );
expect( ctrl.foo ).to.equal( 'hello' );

expect(secondExp).to.equal('oneAlias');
expect( ctrl.one ).to.equal( undefined );
secondList( 'hello one' );
expect( ctrl.one ).to.equal( 'hello one' );

secondList( 'hello one after digest' );
expect( ctrl.one ).to.equal( 'hello one after digest' );

} );

it( `should set to property function which evaluates expression for @Output`, ()=> {
Expand Down

0 comments on commit 193834b

Please sign in to comment.