2

So from the backend I get a array of objects that look kind of like this

ItemsToAdd 
 {
  Page: MemberPage
  Feature: Search
  Text: "Something to explain said feature"
 }

So i match these values to enums in the frontend and then on for example the memberpage i do this check

private get itemsForPageFeatures(): ItemsToAdd[] {
  return this.items.filter(
    (f) =>
      f.page== Pages.MemberPage &&
      f.feature != null
  );
}

What we get from the backend will change a lot over time and is only the same for weeks at most. So I would like to avoid to have to add the components in the template as it will become dead code fast and will become a huge thing to have to just go around and delete dead code. So preferably i would like to add it using a function and then for example for the search feature i would have a ref on the parent like

<SearchBox :ref="Features.Search" />

and in code just add elements where the ItemsToAdd objects Feature property match the ref

is this possible in Vue? things like appendChild and so on doesn't work in Vue but that is the closest thing i can think of to kind of what I want. This function would basically just loop through the itemsForPageFeatures and add the features belonging to the page it is run on.

For another example how the template looks

<template>
<div class="container-fluid mt-3">
  <div
    class="d-flex flex-row justify-content-between flex-wrap align-items-center"
  >
    <div class="d-align-self-end">
      <SearchBox :ref="Features.Search" />
    </div>
  </div>

  <MessagesFilter
    :ref="Features.MessagesFilter"
  />

  <DataChart
    :ref="Features.DataChart"
  />

So say we got an answer from backend where it contains an object that has a feature property DataChart and another one with Search so now i would want components to be added under the DataChart component and the SearchBox component but not the messagesFilter one as we didnt get that from the backend. But then next week we change in backend so we no longer want to display the Search feature component under searchbox. so we only get the object with DataChart so then it should only render the DataChart one. So the solution would have to work without having to make changes to the frontend everytime we change what we want to display as the backend will only be database configs that dont require releases.

Closest i can come up with is this function that does not work for Vue as appendChild doesnt work there but to help with kind of what i imagine. So the component to be generated is known and will always be the same type of component. It is where it is to be placed that is the dynamic part.

private showTextBoxes() {
  this.itemsForPageFeatures.forEach((element) => {
    let el = this.$createElement(NewMinorFeatureTextBox, {
      props: {
        item: element,
      },
    });

    var ref = `${element.feature}`

    this.$refs.ref.appendChild(el);
  });
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.