I'm trying to update a data grid made with Vue.js but the content isn't updating properly. Here's my HTML:
<div class="col-md-10 col-10">
<div class="row" id="grid">
<div class="col-md-4" v-for="entry in entries">
<div class="info_overlay">
<div>
<span class="name">{{ entry.name }}</span>
<span class="description">{{ entry.description }}</span>
</div>
</div>
</div>
</div>
</div>
And now this my JS:
var _results = [{name: 'toto', description: "titi" }];
var app = new Vue({
el: '#grid',
data: {
entries: _results
}
})
socket.on('get_entries', function(data){
console.log(_results);
console.log(data);
// Both logs show the same result (see below)
_results[0].description = data[0].description // This works! The description of the 1st item is updated
_results = data; // This doesn't work
});
Now I don't know why it's not possible to update the whole array at once. I do notice in Chrome a slight difference between the logs messages although the data is the same:
- The first one looks like this: {…}, ob: Se] 0: description: (...) name: (...)
- The second one looks more natural: [{…}] 0: description: "A nice pet" name: "Test pet"
Is there a difference between these two?
_resultsis a global outside of Vue's control, so it's not reactive, whileentriesis part of the application's data, so it is reactive. Note that JavaScript array assignments are by reference, so both_resultsandentriesreference the same individual objects. That's why changing_results[0]also changesentries[0]