0

I am using Vue 3.5.13 and I have this computed list:

const entries = computed<Entry[]>(() => {
    const elements = model.value.elements;
    return elements.map((element, index) => {
        return {
            title: "Test" + element.reference,
            config: {
                reference: element.reference,
                onDelete: () => {
                    console.log("Delete", element.reference);
                    onDeleteElement(element.reference);
                },
            },
        };
    });
});

Whenever I am creating a new element, the reference is initially undefined. After sending the request to the backend, the element has a reference. The reference is needed for the onDeleteElement function.

This is happening:

  1. Creating an element inside the model (empty reference for the element)
  2. Request is sent to the backend (with an empty reference for the element)
  3. Get the model from the backend (now with a reference for the element)
  4. The computed is executed and updated the title with the reference, but when i call onDelete it still has an empty reference, as seen in the "Delete"-log

I already tried outsourcing the element.reference into a variable before the return and also tried to do this for the onDelete callback, but neither of it works.

Also i tried to create a new list every time the computed is run and pushing the objects into the list, but the same problem occurs.

Handling this inside a watch is also not solving the problem.

1
  • 2
    I saw that you had a comment explaining how you solved the problem in Staging Ground which I assumed would carry over to here, but apparently that's not how things work. I'd encourage you to re-post it as an answer to your own question in order to help others. Commented Jul 29 at 11:55

1 Answer 1

1

Okay, so it seems like nothing inside the config object is updated. I tried a few different solutions but in the end I simply needed to rerender the component to which the onDelete is passed with every reference update, like this:

<Entry
    v-for="(entry, index) in entries"
    :key="`${index}-${entry.entryActionConfig?.reference}`"
    :entry
></Entry>

-${entry.entryActionConfig?.reference} is the important part in here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.