Skip to content

Instantly share code, notes, and snippets.

@jwmickey
Created June 2, 2020 15:15
Show Gist options
  • Save jwmickey/f20d0611a7765a93549a051dab95b188 to your computer and use it in GitHub Desktop.
Save jwmickey/f20d0611a7765a93549a051dab95b188 to your computer and use it in GitHub Desktop.
Vue.component('spec', {
props: ['code', 'default'],
inject: ['$getSettings'],
computed: {
// details of this are irrelavent to vue, we're just figuring out what to display
getText() {
const settings = this.$getSettings();
if (!settings.hasOwnProperty(this.code)) {
return this.default;
}
let text = settings[this.code].value;
if (settings[this.code].hasOwnProperty('unit') && settings[this.code].unit.length > 0) {
text += ' ' + settings[this.code].unit;
}
return text;
}
},
template: '<span>{{ getText }}</span>'
});
const vm = new Vue({
el: '#spec',
data: {
spec: {
name: '',
settings: {}
}
},
provide: function() {
return {
$getSettings: () => this.getSettings
}
},
computed: {
getSettings() {
return this.spec.settings;
}
}
});
const specFromSource = {
name: 'Test',
settings: {
abc123: {
value: 42,
unit: 'mm'
}
}
}; // in reality, we get spec from window.postMessage
vm.spec = specFromSource; // this triggers an update
// Usage:
// <body>
// <spec code="abc123"></spec> <!-- this should display "42 mm"
// </body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment