-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathdata.ts
38 lines (33 loc) · 1.01 KB
/
data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as Vue from 'vue'
import { VueClass } from './declarations'
import { noop, warn } from './util'
export function collectDataFromConstructor (vm: Vue, Component: VueClass) {
// override _init to prevent to init as Vue instance
Component.prototype._init = function (this: Vue) {
// proxy to actual vm
Object.getOwnPropertyNames(vm).forEach(key => {
Object.defineProperty(this, key, {
get: () => vm[key],
set: value => vm[key] = value
})
})
}
// should be acquired class property values
const data = new Component()
// create plain data object
const plainData = {}
Object.keys(data).forEach(key => {
if (data[key] !== undefined) {
plainData[key] = data[key]
}
})
if (process.env.NODE_ENV !== 'production') {
if (!(Component.prototype instanceof Vue) && Object.keys(plainData).length > 0) {
warn(
'Component class must inherit Vue or its descendant class ' +
'when class property is used.'
)
}
}
return plainData
}