-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(decorators): Rename decorators
- Loading branch information
1 parent
feceaf9
commit cef2a73
Showing
3 changed files
with
58 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./resolveData"; | ||
export * from "./resolve"; | ||
export * from "./resolveData"; | ||
export * from "./state"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,18 @@ | ||
/** | ||
* An ES7 decorator which maps resolve data to a component. | ||
* | ||
* Add this decorator to a property of your component. | ||
* The decorator marks the component's property to receive resolve data. | ||
* | ||
* When resolve data of the same name (token) is found, | ||
* the resolve data will be assigned to the component's property. | ||
* | ||
* #### Example: | ||
* | ||
* The component's properties receive resolve data from the state definition. | ||
* ```js | ||
* @Component({ selector: 'foo' }) | ||
* export class FooComponent { | ||
* @Resolve() resolveToken1; | ||
* @Resolve('resolveToken2') prop2; | ||
* @Input() @Resolve() resolveToken3; | ||
* } | ||
* | ||
* const fooState = { | ||
* name: 'foo', | ||
* component: FooComponent, | ||
* resolve: [ | ||
* { token: 'resolveToken1', deps: [], resolveFn: resolve1Fn }, | ||
* { token: 'resolveToken2', deps: [], resolveFn: resolve2Fn }, | ||
* { token: 'resolveToken3', deps: [], resolveFn: resolve3Fn }, | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* @param token The resolve token to bind to this property | ||
* (if omitted, the property name is used as the token) | ||
*/ | ||
export function Resolve(token?: string): PropertyDecorator { | ||
return function(target, property) { | ||
const inputs = target['$inputs'] = target['$inputs'] || {}; | ||
inputs[token] = property; | ||
import { StateDeclaration } from '../state/interface'; | ||
import { isArray } from '../common/predicates'; | ||
|
||
export function Resolve(resolveConfig: { token?: string, deps?: any[] }): PropertyDecorator { | ||
resolveConfig = resolveConfig || {}; | ||
|
||
return function(target: StateDeclaration, property) { | ||
const resolve = target.resolve = target.resolve || []; | ||
const token = resolveConfig.token || property; | ||
const deps = resolveConfig.deps || []; | ||
|
||
if (!isArray(resolve)) { | ||
throw new Error(`@ResolveData() only supports array style resolve: state: '${target.name}', resolve: ${property}, token: ${token}.`) | ||
} | ||
|
||
resolve.push({ token, deps, resolveFn: target[property] }); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,41 @@ | ||
import { StateDeclaration } from '../state/interface'; | ||
import { isArray } from '../common/predicates'; | ||
|
||
export function ResolveData(resolveConfig: { token?: string, deps?: any[] }): PropertyDecorator { | ||
resolveConfig = resolveConfig || {}; | ||
|
||
return function(target: StateDeclaration, property) { | ||
const resolve = target.resolve = target.resolve || []; | ||
const token = resolveConfig.token || property; | ||
const deps = resolveConfig.deps || []; | ||
|
||
if (!isArray(resolve)) { | ||
throw new Error(`@ResolveData() only supports array style resolve: state: '${target.name}', resolve: ${property}, token: ${token}.`) | ||
} | ||
|
||
resolve.push({ token, deps, resolveFn: target[property] }); | ||
/** | ||
* An ES7 decorator which maps resolve data to a component. | ||
* | ||
* Add this decorator to a property of your component. | ||
* The decorator marks the component's property to receive resolve data. | ||
* | ||
* When resolve data of the same name (token) is found, | ||
* the resolve data will be assigned to the component's property. | ||
* | ||
* #### Example: | ||
* | ||
* The component's properties receive resolve data from the state definition. | ||
* ```js | ||
* @Component({ selector: 'foo' }) | ||
* export class FooComponent { | ||
* @Resolve() resolveToken1; | ||
* @Resolve('resolveToken2') prop2; | ||
* @Input() @Resolve() resolveToken3; | ||
* } | ||
* | ||
* const fooState = { | ||
* name: 'foo', | ||
* component: FooComponent, | ||
* resolve: [ | ||
* { token: 'resolveToken1', deps: [], resolveFn: resolve1Fn }, | ||
* { token: 'resolveToken2', deps: [], resolveFn: resolve2Fn }, | ||
* { token: 'resolveToken3', deps: [], resolveFn: resolve3Fn }, | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* @param token The resolve token to bind to this property | ||
* (if omitted, the property name is used as the token) | ||
*/ | ||
export function ResolveData(token?: string): PropertyDecorator { | ||
return function(target, property: string) { | ||
const inputs = target['__inputs'] = target['__inputs'] || {}; | ||
token = token || property; | ||
inputs[token] = property; | ||
}; | ||
} | ||
} |