How to turn v-on: click="sync" into something automatic that is triggered when opening the html

1

Explaining the problem

I have this javascript that in the html I call v-on:click="sync" it updates the news list, but this news list while I do not click on sync it goes blank, so I'm looking for a way to make v-on:click="sync" on something automatic that when I open the HTML the news list is updated automatically.

The following JavaScript

document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
var vm = new Vue({
        el: '#app',
        data:{
            noticias: []
        },
        methods:{
            sync:function(){
                $.ajax({
                    dataType: 'json',
                    url: 'http://example/read.php',
                    success:function(dados){
                        localStorage.setItem('noticias',JSON.stringify(dados));
                        vm.setNoticias();
                    },
                    error:function(){
                        alert("ocorreu um erro durante a conexão com o servidor!");
                    }
            });
        },
        setNoticias:function(){
            this.noticias = JSON.parse(localStorage.getItem('noticias'));
            console.log(this.noticias);
        }
    },
    ready:function(){
     this.setNoticias();
        }
    })
}
    
asked by anonymous 24.11.2016 / 23:01

1 answer

1

You can take the creation of vm from the function and shortly after calling the function:

var vm = new Vue({...});

vm.setNoticias();
    
27.11.2016 / 00:37