Skip to content

Commit

Permalink
Merge pull request #18429 from dannon/more-robust-pinia-init
Browse files Browse the repository at this point in the history
[24.1] Support usage of vue mounting helpers in a context without existing pinia/vue.
  • Loading branch information
mvdbeek authored Jun 21, 2024
2 parents 4239fe1 + 16b8036 commit ebb2651
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions client/src/utils/mountVueComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import BootstrapVue from "bootstrap-vue";
import { iconPlugin, localizationPlugin, vueRxShortcutPlugin } from "components/plugins";
import { getActivePinia, PiniaVuePlugin } from "pinia";
import { createPinia, getActivePinia, PiniaVuePlugin } from "pinia";
import Vue from "vue";

// Load Pinia
Expand All @@ -22,17 +22,28 @@ Vue.use(vueRxShortcutPlugin);
// font-awesome svg icon registration/loading
Vue.use(iconPlugin);

function getOrCreatePinia() {
// We sometimes use this utility mounting function in a context where there
// is no existing vue application or pinia store (e.g. individual charts
// displayed in an iframe).
// To support both use cases, we will create a new pinia store and attach it to the
// vue application that is created for the component.
let pinia = getActivePinia();
if (!pinia) {
pinia = createPinia();
}
return pinia;
}

export const mountVueComponent = (ComponentDefinition) => {
const component = Vue.extend(ComponentDefinition);
const pinia = getActivePinia();
return (propsData, el) => new component({ propsData, el, pinia });
return (propsData, el) => new component({ propsData, el, pinia: getOrCreatePinia() });
};

export const replaceChildrenWithComponent = (el, ComponentDefinition, propsData = {}) => {
const container = document.createElement("div");
el.replaceChildren(container);
const component = Vue.extend(ComponentDefinition);
const pinia = getActivePinia();
const mountFn = (propsData, el) => new component({ propsData, el, pinia });
const mountFn = (propsData, el) => new component({ propsData, el, pinia: getOrCreatePinia() });
return mountFn(propsData, container);
};

0 comments on commit ebb2651

Please sign in to comment.