2

So, in one of my VueJS templates, I have a left sidebar that generates buttons by iterating (v-for) through a multidimensional items array.

When one of these buttons is clicked, a method is run:

this.active.notes = item.notes

active.notes is bound to a textarea in the right content section.

So, every time you click one of the item buttons, you see the (active) notes associated with that item.

I want to be able to have the user edit the active notes in the textarea. I have an AJAX call on textarea blur which updates the db. But the problem is, the items data hasn't changed. So if I click a different item, then click back to the edited item, I see the pre-edited notes. When I refresh the page, of course, everything lines up perfectly.

What is the best way to update the items data, so that it is always consistent with the textarea edits? Should I reload the items data somehow (with another AJAX call to the db)? Or is there a better way to bind the models together?

Here is the JS:

export default {
    mounted () {
        this.loadItems();
    },

    data() {
        return {
            items: [],
            active: {
                notes: ''
            },
        }
    },

    methods: {

        loadItems() {

            axios.get('/api/items/'+this.id)
                    .then(resp => {
                        this.items = resp.data
                    })

        },

        saveNotes () {

           ...api call to save in db...

        },

        updateActive (item) {
            this.active.notes = item.notes;
        },
    }
}
2
  • In saveNotes, can't you just update the internal state? So not only send the new text to the database, but also storing it in this.active.notes? (Or better, storing it in this.active.notes once the database confirmed it successfully stored the data) Commented Mar 2, 2017 at 21:29
  • @cello Yes, I actually do also store it in this.active.notes, but it doesn't help, because when I click a different item, the active.notes values changes. I need to change an element in the items data or refresh the data, I think Commented Mar 2, 2017 at 22:27

3 Answers 3

3

i can't find items property in your data object.

a property must be present in the data object in order for Vue to convert it and make it reactive

Vue does not allow dynamically adding new root-level reactive properties to an already created instance

maybe you can have a look at this:

Vue Reactivity in Depth

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

Comments

0

It doesn't seem like this.items exists in your structure, unless there is something that isn't shown. If it doesn't exist set it as an empty array, which will be filled on your ajax call:

data() {
    return {
        active: {
            notes: ''
        },
        items: [],
},

Now when you ajax method runs, the empty array, items, will be filled with your resp.data via this line:(this.items = resp.data). Then you should be able to iterate through your items array using v-for and your updateActive method should work as you intend it to.

4 Comments

Yeah, 'items' is actually in the data section. But it doesn't update after a second AJAX call. What's the best way to update data properties via AJAX?
@ΓΓΓICK can you edit your question to show items in the data structure?
OK, just did. That's the way it looks in the code, and it still isn't working. Should it be? I don't get it.
@ΓΓΓICK can you console.log your resp.data from your success statement?
0

use PUSH

this.items.push(resp.data);

here is a similar question vue.js http get web api url render list

Comments

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.