-
Notifications
You must be signed in to change notification settings - Fork 0
/
MnToVue.vue
66 lines (53 loc) · 1.63 KB
/
MnToVue.vue
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
<template>
<div class="mn-to-vue-layout"></div>
</template>
<script>
import Mn from 'backbone.marionette';
import _ from 'lodash';
export default {
name: 'MnToVue',
props: {
options: Object,
MnComponent: { required: true, type: Function }
},
data() {
return { mn: null, region: null };
},
watch: {
/**
* @param {{}} newOptions
* @param {{}} oldOptions
*/
options(newOptions, oldOptions) {
if (this.mn) {
const keys = _.union(Object.keys(oldOptions), Object.keys(newOptions));
keys.forEach(key => {
if (!_.isEqual(oldOptions[key], newOptions[key])) {
this.mn.options[key] = newOptions[key];
this.mn.triggerMethod(`options:${key}:updated`, newOptions[key]);
}
});
}
}
},
/**
* @throws {Error}
*/
async mounted() {
await this.$nextTick();
this.region = new Mn.Region({ el: this.$el });
const mn = new this.MnComponent(this.options);
mn.on('all', (...payload) => this.$emit(...payload));
this.mn = mn;
this.region.show(mn);
},
beforeDestroy() {
if (this.region) {
this.region.reset();
this.region.destroy();
this.mn = null;
this.region = null;
}
}
};
</script>