hubl/src/components/hubl-reactivity.js

42 lines
1.0 KiB
JavaScript

import {
store,
Sib,
StoreMixin
} from 'https://cdn.skypack.dev/@startinblox/core@0.17';
export const HublReactivity = {
name: 'hubl-reactivity',
use: [StoreMixin],
attributes: {
targetSrc: {
type: String,
default: '',
callback: function () {
this.subscribe();
}
},
refreshData: {
type: String,
},
},
async fetchData(value) {
this.resourceId = null;
if (this.nestedField) {
const resource = store.get(value) || await store.getData(value, this.context);
const nestedResource = await resource[this.nestedField]
this.resourceId = nestedResource ? nestedResource['@id'] : null;
} else {
this.resourceId = value;
}
this.subscribe();
},
subscribe() {
if (this.refreshData === '') { // clears cache and re-fetch data
store.subscribeVirtualContainerTo(this.resourceId, this.targetSrc);
} else { // notifies components
store.subscribeResourceTo(this.resourceId, this.targetSrc);
}
}
}
Sib.register(HublReactivity);