Skip to content

Commit

Permalink
fix(directives/directive_provider): correctly assign controller do DDO
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Jan 10, 2016
1 parent c78cc9c commit d839757
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/core/di/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ class ProviderBuilder{
}

if ( annotation instanceof PipeMetadata ) {
return pipeProvider.createFromType(injectableType);
return pipeProvider.createFromType( injectableType );
}

if ( annotation instanceof DirectiveMetadata ) {
return directiveProvider.createFromType(injectableType);
return directiveProvider.createFromType( injectableType );
}

if ( annotation instanceof InjectableMetadata ) {
Expand Down
30 changes: 18 additions & 12 deletions src/core/directives/directive_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,40 @@ export class DirectiveProvider {
const {inputs,attrs,outputs,host,queries,legacy} = metadata;

let _ddo = {} as ng.IDirective;
const _basicDDO = {
controller: type,
require: this._createRequires( requireMap, directiveName ),
} as ng.IDirective;

if ( metadata instanceof ComponentMetadata ) {

_ddo = {
const componentSpecificDDO = {
scope: {},
bindToController: this._createComponentBindings( inputs, attrs, outputs ),
controller: Type,
controllerAs: 'ctrl',
require: this._createRequires( requireMap, directiveName ),
link: this._createLink( type, metadata, lfHooks, requireMap )
};
} as ng.IDirective;

if ( metadata.template && metadata.templateUrl ) {
throw new Error( 'cannot have both template and templateUrl' );
}
if ( metadata.template ) {
_ddo.template = metadata.template;
componentSpecificDDO.template = metadata.template;
}
if ( metadata.templateUrl ) {
_ddo.templateUrl = metadata.templateUrl;
componentSpecificDDO.templateUrl = metadata.templateUrl;
}

}
else {
_ddo = {
controller: Type,
require: this._createRequires( requireMap, directiveName ),
StringMapWrapper.assign( _ddo, _basicDDO, componentSpecificDDO );

} else {

const directiveSpecificDDO = {
link: this._createLink( type, metadata, lfHooks, requireMap )
};
} as ng.IDirective;

StringMapWrapper.assign( _ddo, _basicDDO, directiveSpecificDDO );

}

const ddo = this._createDDO( _ddo, metadata.legacy );
Expand Down
5 changes: 5 additions & 0 deletions test/core/di/povider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe( `di/provider`, ()=> {
expect( FooPipe.$inject ).to.deep.equal( [ 'myService' ] );

} );

it( `should return string name and directiveFactory for Angular registry and add $inject prop if needed (Directive)`, ()=> {

class MyService{}
Expand All @@ -102,9 +103,12 @@ describe( `di/provider`, ()=> {

expect( ngContainerName ).to.deep.equal( 'myFoo' );
expect( isFunction( filterFactory ) ).to.deep.equal( true );

expect(FooDirective.$inject).to.deep.equal(['myService']);
//expect( filterFactory().controller ).to.equal( FooDirective );

} );

it( `should return string name and directiveFactory for Angular registry and add $inject prop if needed (Component)`, ()=> {

class MyService{}
Expand All @@ -124,6 +128,7 @@ describe( `di/provider`, ()=> {
expect( isFunction( filterFactory ) ).to.deep.equal( true );

expect(FooComponent.$inject).to.deep.equal(['myService','$element']);
expect( filterFactory().controller ).to.equal( FooComponent );

} );

Expand Down
4 changes: 2 additions & 2 deletions test/core/directives/directive_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe( `directives/directive_provider`, ()=> {
expect( isFunction( directiveFactory ) ).to.equal( true );
expect( directiveFactory() ).to.deep.equal( {
require: [ 'myClicker' ],
controller: MyClicker.constructor,
controller: MyClicker,
link: {
pre: (ddo.link as ng.IDirectivePrePost).pre,
post: (ddo.link as ng.IDirectivePrePost).post
Expand Down Expand Up @@ -209,7 +209,7 @@ describe( `directives/directive_provider`, ()=> {
onLightsaberAttack: '&'
},
require: [ 'jediMaster' ],
controller: JediMasterCmp.constructor,
controller: JediMasterCmp,
controllerAs: 'ctrl',
link: {
pre: (ddo.link as ng.IDirectivePrePost).pre,
Expand Down

0 comments on commit d839757

Please sign in to comment.