forked from alexjoverm/v-runtime-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (48 loc) · 1.32 KB
/
index.js
File metadata and controls
55 lines (48 loc) · 1.32 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const getKeysFromOptions = options => [
...Object.keys((options.data && options.data()) || {}),
...Object.keys(options.props || {})
];
const defineDescriptor = (src, dest, name) => {
if (!dest.hasOwnProperty(name)) {
const descriptor = Object.getOwnPropertyDescriptor(src, name);
Object.defineProperty(dest, name, descriptor);
}
};
const merge = objs => {
const res = {};
objs.forEach(obj => {
obj &&
Object.getOwnPropertyNames(obj).forEach(name =>
defineDescriptor(obj, res, name)
);
});
return res;
};
const buildFromProps = (obj, props) => {
const res = {};
props.forEach(prop => defineDescriptor(obj, res, prop));
return res;
};
export default {
props: {
template: String
},
render(h) {
if (this.template) {
const { $data, $props, $options } = this.$parent.$parent;
const methodKeys = Object.keys($options.methods || {});
const allKeys = getKeysFromOptions($options).concat(methodKeys);
const methods = buildFromProps(this.$parent.$parent, methodKeys);
const props = merge([$data, $props, methods]);
const dynamic = {
template: this.template || "<div></div>",
props: allKeys,
computed: $options.computed,
components: $options.components
};
return h(dynamic, {
props
});
}
}
};