2

On page load I receive an array of objects from the server, which are displayed using a v-for loop.

The application is real time, so when another user updates an object in the array, I receive a pusher event which contains the updated object (this is a Laravel 5.4 broadcast event containing the updated model).

How do I then locate that object within the array to replace it with the updated version?

Example component JS

export default{
    data(){
        return {
            items: []
        }
    },

    mounted(){
       ...some code to get the array from the server

       this.items = server.response;
    },

    created() {
         Echo.private('channel-name)
           .listen('event', (event) => {

               **What do I do here with the received model to update the items array?**

            });
        });
    }
}

1 Answer 1

2

I think you should have some ID inside your item entity and then just lookup for the item with the same ID and replace it. Replacing can be done, for example, using map:

...
//assuming you have ID inside your item entity
//and your new "replacement" model is in event.item

.listen('event', (event) => {
  var newItem = event.item;

  this.items = this.items.map(item => {
   return item.ID === newItem.ID ? newItem : item;
  });
});
...
Sign up to request clarification or add additional context in comments.

1 Comment

It appears that I was overthinking it. Thanks!

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.