Commit ec49429e authored by Andrew Fontaine's avatar Andrew Fontaine

Update Vue Documentation to Use Plugin/Mixin

This updates our documentation to ensure people are aware of the now
existing `GlFeatureFlagsPlugin` and `glFeatureFlagsMixin` to easily
provide and inject the feature flags object into a child component.
parent 02b3fbc5
import Vue from 'vue'; import Vue from 'vue';
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
Vue.config.productionTip = false; Vue.config.productionTip = false;
} }
Vue.use(GlFeatureFlagsPlugin);
export default Vue => { export default Vue => {
Vue.mixin({ Vue.mixin({
provide: { provide: {
glFeatures: { ...(window.gon.features || {}) }, glFeatures: { ...((window.gon && window.gon.features) || {}) },
}, },
}); });
}; };
...@@ -2,7 +2,7 @@ export default () => ({ ...@@ -2,7 +2,7 @@ export default () => ({
inject: { inject: {
glFeatures: { glFeatures: {
from: 'glFeatures', from: 'glFeatures',
default: {}, default: () => ({}),
}, },
}, },
}); });
...@@ -108,42 +108,44 @@ document.addEventListener('DOMContentLoaded', () => new Vue({ ...@@ -108,42 +108,44 @@ document.addEventListener('DOMContentLoaded', () => new Vue({
Use Vue's [provide/inject](https://vuejs.org/v2/api/#provide-inject) mechanism Use Vue's [provide/inject](https://vuejs.org/v2/api/#provide-inject) mechanism
to make feature flags available to any descendant components in a Vue to make feature flags available to any descendant components in a Vue
application: application. The `glFeatures` object is already provided in `commons/vue.js`, so
only the mixin is required to utilize the flags:
```javascript ```javascript
// application entry point
document.addEventListener('DOMContentLoaded', () => new Vue({
el: '.js-vue-app',
provide: {
aFeatureFlag: gon.features.aFeatureFlag,
},
render(createElement) {
return createElement('my-component');
},
}));
// An arbitrary descendant component // An arbitrary descendant component
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
export default { export default {
... // ...
inject: { mixins: [glFeatureFlagsMixin()],
aFeatureFlag: { // ...
from: 'aFeatureFlag', created() {
default: false, if (this.glFeatures.myFlag) {
}, // ...
}
}, },
...
} }
``` ```
This approach has several benefits: This approach has a few benefits:
- Arbitrarily deeply nested components can opt-in and access the flag without - Arbitrarily deeply nested components can opt-in and access the flag without
intermediate components being aware of it (c.f. passing the flag down via intermediate components being aware of it (c.f. passing the flag down via
props). props).
- Components can use a different local name for the flag if necessary.
- A default value can be declared in case the flag is not provided.
- Good testability, since the flag can be provided to `mount`/`shallowMount` - Good testability, since the flag can be provided to `mount`/`shallowMount`
from `vue-test-utils` as easily as a prop. from `vue-test-utils` as easily as a prop.
```javascript
import { shallowMount } from '@vue/test-utils';
shallowMount(component, {
provide: {
glFeatures: { myFlag: true },
},
});
```
- No need to access a global variable, except in the application's - No need to access a global variable, except in the application's
[entry point](#accessing-the-gl-object). [entry point](#accessing-the-gl-object).
......
...@@ -110,7 +110,7 @@ The name of the feature flag in JavaScript will always be camelCased, meaning ...@@ -110,7 +110,7 @@ The name of the feature flag in JavaScript will always be camelCased, meaning
that checking for `gon.features.vim_bindings` would not work. that checking for `gon.features.vim_bindings` would not work.
See the [Vue guide](../fe_guide/vue.md#accessing-feature-flags) for details about See the [Vue guide](../fe_guide/vue.md#accessing-feature-flags) for details about
how to access feature flags in a Vue application. how to access feature flags in a Vue component.
### Specs ### Specs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment