I have been tinkering with a JavaScript object that I want to connect with a Vue view.
When I trigger a function to update this JavaScript object using AJAX, I expected Vue to automatically sync up and refresh the view. However, it seems like the binding isn't happening as anticipated.
Some research advises making the AJAX call within the Vue declaration itself, but due to certain limitations, I am reluctant to take that route.
To showcase the problem more visually, I've put together a fiddle demonstrating the issue without the AJAX component. You can also find the relevant code posted below.
https://jsfiddle.net/g6u2tph7/5/
Thank you in advance for your assistance and insights.
Best regards,
vmitchell85
JavaScript
window.changeTheData = function (){
externalJSSystems = [{description: 'Baz'}, {description: 'Car'}];
document.getElementById("log").innerHTML = 'function has ran...';
// This doesn't update the Vue data
}
var externalJSSystems = [{description: 'Foo'}, {description: 'Bar'}];
Vue.component('systable', {
template: '#sysTable-template',
data() {
return {
systems: externalJSSystems
};
}
});
new Vue({
el: 'body'
});
HTML
<systable :systems="systems"></systable>
<button type="button" onclick="changeTheData()">Change</button>
<br><br>
<div id="log"></div>
<template id="sysTable-template">
<table>
<thead>
<tr>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr v-for="sys in systems">
<td>{{ sys.description }}</td>
</tr>
</tbody>
</table>
</template>