Skip to content

fix: allow to create a custom class decorator #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { DecoratedClass } from './declarations'

export const noop = () => {}

export function createDecorator (
factory: (options: ComponentOptions<any, any, any, any>, key: string) => void
): (target: Vue, key: string) => void
export function createDecorator (
factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void
): (target: Vue, key: string, index: number) => void
export function createDecorator (
factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void
): (target: Vue, key: string, index: any) => void {
return (target, key, index) => {
const Ctor = target.constructor as DecoratedClass
export interface VueDecorator {
// Class decorator
(Ctor: typeof Vue): void

// Property decorator
(target: Vue, key: string): void

// Parameter decorator
(target: Vue, key: string, index: number): void
}

export function createDecorator (factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void): VueDecorator {
return (target: Vue | typeof Vue, key?: any, index?: any) => {
const Ctor = typeof target === 'function'
? target as DecoratedClass
: target.constructor as DecoratedClass
if (!Ctor.__decorators__) {
Ctor.__decorators__ = []
}
Expand Down
18 changes: 18 additions & 0 deletions test/test-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ describe('vue-class-component with Babel', () => {
console.warn = originalWarn
}
})

// #155
it('createDecrator: create a class decorator', () => {
const DataMixin = createDecorator(options => {
options.data = function () {
return {
test: 'foo'
}
}
})

@Component
@DataMixin
class MyComp extends Vue {}

const vm = new MyComp()
expect(vm.test).to.equal('foo')
})
})
18 changes: 18 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,22 @@ describe('vue-class-component', () => {
expect(parent.value).to.equal('parent')
expect(child.value).to.equal('child')
})

// #155
it('createDecrator: create a class decorator', () => {
const DataMixin = createDecorator(options => {
options.data = function () {
return {
test: 'foo'
}
}
})

@Component
@DataMixin
class MyComp extends Vue {}

const vm: any = new MyComp()
expect(vm.test).to.equal('foo')
})
})