-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSvelteComponent.js
82 lines (71 loc) · 2.05 KB
/
SvelteComponent.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
73
74
75
76
77
78
79
80
81
82
/**
* Blaze template that embeds a Svelte component.
*/
import { Template } from 'meteor/templating';
import { SvelteComponent } from 'svelte/internal';
Template.__checkName("SvelteComponent");
Template.SvelteComponent = new Template("Template.SvelteComponent", () => []);
Template.SvelteComponent.onRendered(function onRendered() {
const options = {
target: this.view._domrange.parentElement,
anchor: this.view._domrange.lastNode(),
props: {},
};
if (this.view.templateContentBlock) {
options.props.$$slots = {default: [create_default_slot]};
options.props.$$scope = {ctx: [this.view.templateContentBlock]};
}
if (this.data.prototype instanceof SvelteComponent) {
// {{> SvelteComponent SomeComponent}}
this.component = new this.data(options);
}
else if (this.data.this === undefined) {
// DEPRECATED syntax
// {{> SvelteComponent component=SomeComponent props=helper}}
if (this.data.props) {
_.extend(options.props, this.data.props);
}
this.component = new this.data.component(options);
let initial = true;
Tracker.autorun(() => {
const data = Blaze.getData(this.view);
if (!initial) {
this.component.$set(data.props || {});
}
});
initial = false;
}
else {
// New syntax
// {{> SvelteComponent this=SomeComponent prop1="value" prop2="value"}}
_.extend(options.props, _.omit(this.data, 'this'));
this.component = new this.data.this(options);
let initial = true;
Tracker.autorun(() => {
const data = Blaze.getData(this.view);
if (!initial) {
this.component.$set(_.omit(data, 'this'));
}
});
initial = false;
}
});
Template.SvelteComponent.onDestroyed(function onDestroyed() {
this.component.$destroy();
});
function create_default_slot(ctx) {
let blazeView;
return {
c() {
},
m(target, anchor) {
const templ = ctx[0];
blazeView = Blaze.render(templ.constructView(), target, anchor);
},
d(detaching) {
if (detaching) {
Blaze.remove(blazeView);
}
}
};
}