forked from alexjoverm/v-runtime-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (63 loc) · 2.55 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
const { components = {}, computed = {}, methods = {} } = $options;
let passthrough = {$data:{}, $props:{}, $options:{}, components:{}, computed:{}, methods:{}};
if (typeof this.$options.methods === "undefined") {
this.$options.methods = {};
}
if (typeof this.$options.computed === "undefined") {
this.$options.computed = {};
}
if (typeof this.$options.components === "undefined") {
this.$options.components = {};
}
//build new objects by removing keys if already exists (e.g. created by mixins)
Object.keys($data).forEach(e => {if(typeof this.$data[e]==="undefined") passthrough.$data[e] = $data[e];} );
Object.keys($props).forEach(e => {if(typeof this.$props[e]==="undefined") passthrough.$props[e] = $props[e];} );
Object.keys(methods).forEach(e => {if(typeof this.$options.methods[e]==="undefined") passthrough.methods[e] = methods[e];} );
Object.keys(computed).forEach(e => {if(typeof this.$options.computed[e]==="undefined") passthrough.computed[e] = computed[e];} );
Object.keys(components).forEach(e => {if(typeof this.$options.components[e]==="undefined") passthrough.components[e] = components[e];} );
const methodKeys = Object.keys(passthrough.methods || {});
const dataKeys = Object.keys(passthrough.$data || {});
const propKeys = Object.keys(passthrough.$props || {});
const allKeys = dataKeys.concat(propKeys).concat(methodKeys);
const methodsFromProps = buildFromProps(this.$parent, methodKeys);
const props = merge([passthrough.$data, passthrough.$props, methodsFromProps]);
const dynamic = {
template: this.template || "<div></div>",
props: allKeys,
computed: passthrough.computed,
components: passthrough.components
};
return h(dynamic, {
props
});
}
}
};