Skip to content

Commit

Permalink
Merge pull request #5 from vsternbach/component-directive
Browse files Browse the repository at this point in the history
Add attr selector support for @component
  • Loading branch information
Vlad Sternbach authored Apr 20, 2017
2 parents 9514a4a + a79edb8 commit 5f565bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ts-decorators",
"version": "0.0.38",
"version": "0.1.0",
"license": "MIT",
"author": {
"name": "Vlad Sternbach",
Expand Down
29 changes: 26 additions & 3 deletions src/angular-ts-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface ComponentOptionsDecorated {
templateUrl?: string | ng.Injectable<(...args: Array<any>) => string>;
transclude?: boolean | {[slot: string]: string};
require?: {[controller: string]: string};
controllerAs?: string;
restrict?: string;
/**
* @deprecated
*/
replace?: boolean;
}

export interface DirectiveOptionsDecorated {
Expand All @@ -59,6 +65,8 @@ export interface DirectiveOptionsDecorated {
templateUrl?: string | ((tElement: JQuery, tAttrs: ng.IAttributes) => string);
terminal?: boolean;
transclude?: boolean | 'element' | {[slot: string]: string};
controllerAs?: string;
restrict?: string;
}

export interface DirectiveControllerConstructor {
Expand Down Expand Up @@ -153,12 +161,27 @@ export function Component(decoratedOptions: ComponentOptionsDecorated) {
return (ctrl: ng.IControllerConstructor) => {
const options: ng.IComponentOptions = {...decoratedOptions};
options.controller = ctrl;

const isAttrSelector = /^[\[].*[\]]$/g.test(decoratedOptions.selector);

const bindings = Reflect.getMetadata(bindingsSymbol, ctrl);
if (bindings) {
options.bindings = bindings;
if (isAttrSelector) {
options['bindToController'] = bindings;
options['controllerAs'] = options['controllerAs'] || '$ctrl';
}
else options['bindings'] = bindings;
}
Reflect.defineMetadata(nameSymbol, decoratedOptions.selector, ctrl);
Reflect.defineMetadata(typeSymbol, Declarations.component, ctrl);

if (isAttrSelector) {
(<ng.IDirective>options).restrict = 'A';
}

const selector = isAttrSelector ?
decoratedOptions.selector.substr(1, decoratedOptions.selector.length - 2) : decoratedOptions.selector;

Reflect.defineMetadata(nameSymbol, selector, ctrl);
Reflect.defineMetadata(typeSymbol, isAttrSelector ? Declarations.directive : Declarations.component, ctrl);
Reflect.defineMetadata(optionsSymbol, options, ctrl);
};
}
Expand Down

0 comments on commit 5f565bc

Please sign in to comment.