-
Notifications
You must be signed in to change notification settings - Fork 0
/
VueToMn.js
60 lines (48 loc) · 1.17 KB
/
VueToMn.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
import _ from 'lodash';
import Mn from 'backbone.marionette';
const DEFAULT_OPTIONS = () => ({
template: () => null,
hoistEvents: []
});
/**
* @param {Vue} AppVue
* @returns {function(VueComponent): Mn.LayoutView}
*/
export const wrapper = AppVue => vueComponent => class extends Mn.LayoutView {
constructor(options) {
super(options);
this._root = this._initRoot();
}
/**
* @returns {{}}
*/
options() {
return DEFAULT_OPTIONS();
}
onAttach() {
this._root.$mount(this.el);
}
onBeforeDestroy() {
this._root.$destroy();
}
/**
* @returns {VueComponent}
* @private
*/
_initRoot() {
const RootComp = AppVue.extend(vueComponent);
const root = new RootComp({
propsData: _.omit(this.options, 'template', 'hoistEvents'),
});
this._hoistEventsToParent(root);
return root;
}
_hoistEventsToParent(vm) {
this.options.hoistEvents.forEach(
event => this._hoistEvent(vm, event)
);
}
_hoistEvent(vm, event) {
vm.$on(event, (...all) => this.triggerMethod(event, ...all));
}
};