Skip to content
Snippets Groups Projects
kb-reactivity.js 1.38 KiB
import { store } from 'https://unpkg.com/@startinblox/core@0.13';
import { Sib } from "https://unpkg.com/@startinblox/core@0.13/dist/libs/Sib.js";
import { StoreMixin } from "https://unpkg.com/@startinblox/core@0.13/dist/mixins/storeMixin.js";

// import { store } from '/scripts/sib-core/dist/index.js';
// import { Sib } from "/scripts/sib-core/dist/libs/Sib.js";
// import { StoreMixin } from "/scripts/sib-core/dist/mixins/storeMixin.js";

export const KbReactivity = {
  name: 'kb-reactivity',
  use: [StoreMixin],
  attributes: {
    targetSrc: {
      type: String,
      default: '',
      callback: function () {
        this.subscribe();
      }
    },
    dataSrc: {
      type: String,
      default: '',
      callback: async function (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.resourceId && this.targetSrc) {
      store.subscribeVirtualContainerTo(this.resourceId, this.targetSrc);
      store.subscribeVirtualContainerTo(this.targetSrc, this.resourceId);
    }
  }
}

Sib.register(KbReactivity);