File tree 7 files changed +77
-14
lines changed
7 files changed +77
-14
lines changed Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' )
2
+ const spawn = require ( 'child_process' ) . spawn
3
+
4
+ run ( 'tsc -w -p .' )
5
+ run ( 'webpack --config test/webpack.config.js --watch' )
6
+
7
+ fs . watch ( 'test/test.build.js' , ( ) => {
8
+ run ( 'mocha --reporter min test/test.build.js' )
9
+ } )
10
+
11
+ function run ( command ) {
12
+ const [ name , ...args ] = command . split ( ' ' )
13
+ spawn ( `node_modules/.bin/${ name } ` , args , {
14
+ shell : true ,
15
+ stdio : 'inherit'
16
+ } )
17
+ }
Original file line number Diff line number Diff line change 15
15
"clean" : " rimraf ./lib" ,
16
16
"example" : " npm run build && webpack --config example/webpack.config.js" ,
17
17
"dev" : " webpack --config example/webpack.config.js --watch" ,
18
+ "dev:test" : " node build/dev-test.js" ,
18
19
"test" : " npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js"
19
20
},
20
21
"repository" : {
Original file line number Diff line number Diff line change 1
1
import Vue , { ComponentOptions } from 'vue'
2
- import { VueClass } from './declarations'
2
+ import { VueClass , DecoratedClass } from './declarations'
3
3
import { collectDataFromConstructor } from './data'
4
4
5
5
export const $internalHooks = [
@@ -17,11 +17,6 @@ export const $internalHooks = [
17
17
'render'
18
18
]
19
19
20
- // Property, method and parameter decorators created by `createDecorator` helper
21
- // will enqueue functions that update component options for lazy processing.
22
- // They will be executed just before creating component constructor.
23
- export let $decoratorQueue : ( ( options : ComponentOptions < Vue > ) => void ) [ ] = [ ]
24
-
25
20
export function componentFactory (
26
21
Component : VueClass ,
27
22
options : ComponentOptions < any > = { }
@@ -59,9 +54,10 @@ export function componentFactory (
59
54
} )
60
55
61
56
// decorate options
62
- $decoratorQueue . forEach ( fn => fn ( options ) )
63
- // reset for other component decoration
64
- $decoratorQueue = [ ]
57
+ const decorators = ( Component as DecoratedClass ) . __decorators__
58
+ if ( decorators ) {
59
+ decorators . forEach ( fn => fn ( options ) )
60
+ }
65
61
66
62
// find super
67
63
const superProto = Object . getPrototypeOf ( Component . prototype )
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
3
3
export type VueClass = { new ( ) : Vue } & typeof Vue
4
+
5
+ export type DecoratedClass = VueClass & {
6
+ // Property, method and parameter decorators created by `createDecorator` helper
7
+ // will enqueue functions that update component options for lazy processing.
8
+ // They will be executed just before creating component constructor.
9
+ __decorators__ ?: ( ( options : Vue . ComponentOptions < Vue > ) => void ) [ ]
10
+ }
Original file line number Diff line number Diff line change 1
1
import Vue , { ComponentOptions } from 'vue'
2
- import { $decoratorQueue } from './component '
2
+ import { DecoratedClass } from './declarations '
3
3
4
4
export const noop = ( ) => { }
5
5
@@ -12,11 +12,15 @@ export function createDecorator (
12
12
export function createDecorator (
13
13
factory : ( options : ComponentOptions < Vue > , key : string , index : number ) => void
14
14
) : ( target : Vue , key : string , index : any ) => void {
15
- return ( _ , key , index ) => {
15
+ return ( target , key , index ) => {
16
+ const Ctor = target . constructor as DecoratedClass
17
+ if ( ! Ctor . __decorators__ ) {
18
+ Ctor . __decorators__ = [ ]
19
+ }
16
20
if ( typeof index !== 'number' ) {
17
21
index = undefined
18
22
}
19
- $decoratorQueue . push ( options => factory ( options , key , index ) )
23
+ Ctor . __decorators__ . push ( options => factory ( options , key , index ) )
20
24
}
21
25
}
22
26
Original file line number Diff line number Diff line change 1
- import Component , { createDecorator } from '../'
1
+ import Component , { createDecorator } from '../lib '
2
2
import chai , { expect } from 'chai'
3
3
import spies from 'chai-spies'
4
4
import Vue from 'vue'
Original file line number Diff line number Diff line change 1
- import Component , { createDecorator } from '../'
1
+ import Component , { createDecorator } from '../lib '
2
2
import { expect } from 'chai'
3
3
import Vue from 'vue'
4
4
@@ -218,4 +218,42 @@ describe('vue-class-component', () => {
218
218
expect ( c . bar ) . to . equal ( 'world' )
219
219
expect ( ( MyComp as any ) . options . computed . bar . cache ) . to . be . false
220
220
} )
221
+
222
+ // #104
223
+ it ( 'createDecorator: decorate correctly even if a component is created in another @Component decorator' , ( ) => {
224
+ // Just assigns the given value to the decorated property
225
+ const Value = ( value : any ) => createDecorator ( ( options , key ) => {
226
+ const data = options . data as Function || ( ( ) => ( { } ) )
227
+ options . data = function ( ) {
228
+ return {
229
+ ...data . call ( this ) ,
230
+ [ key ] : value
231
+ }
232
+ }
233
+ } )
234
+
235
+ const createChild = ( ) => {
236
+ @Component
237
+ class Child extends Vue {
238
+ @Value ( 'child' )
239
+ value : string
240
+ }
241
+ return Child
242
+ }
243
+
244
+ @Component ( {
245
+ components : {
246
+ Child : createChild ( )
247
+ }
248
+ } )
249
+ class Parent extends Vue {
250
+ @Value ( 'parent' )
251
+ value : string
252
+ }
253
+
254
+ const parent = new Parent ( )
255
+ const child = new ( parent as any ) . $options . components . Child ( )
256
+ expect ( parent . value ) . to . equal ( 'parent' )
257
+ expect ( child . value ) . to . equal ( 'child' )
258
+ } )
221
259
} )
You can’t perform that action at this time.
0 commit comments