La función Vue js Ready no se activa
Tengo esta función vue donde básicamente hay dos métodos. El primeropostStatus
se usa para guardar una publicación justo después de que el usuario hace clic en el botón Guardar, y la otragetPosts
se utiliza para recuperar todas las publicaciones anteriores de ese usuario de la base de datos.
Aquí está el vue.js, donde hay una llamada ajax a un controlador (en Laravel 5.3)
$(document).ready(function () {
var csrf_token = $('meta[name="csrf-token"]').attr('content');
/*Event handling within vue*/
//when we actually submit the form, we want to catch the action
new Vue({
el : '#timeline',
data : {
post : '',
posts : [],
token : csrf_token,
limit : 20,
},
methods : {
postStatus : function (e) {
e.preventDefault();
//console.log('Posted: '+this.post+ '. Token: '+this.token);
var request = $.ajax({
url : '/posts',
method : "POST",
dataType : 'json',
data : {
'body' : this.post,
'_token': this.token,
}
}).done(function (data) {
//console.log('Data saved successfully. Response: '+data);
this.post = '';
this.posts.unshift(data); //Push it to the top of the array and pass the data that we get back
}.bind(this));/*http://stackoverflow.com/a/26479602/1883256 and http://stackoverflow.com/a/39945594/1883256 */
/*request.done(function( msg ) {
console.log('The tweet has been saved: '+msg+'. Outside ...');
//$( "#log" ).html( msg );
});*/
request.fail(function( jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );
});
},
getPosts : function () {
//Ajax request to retrieve all posts
$.ajax({
url : '/posts',
method : "GET",
dataType : 'json',
data : {
limit : this.limit,
}
}).done(function (data) {
this.posts = data.posts;
}.bind(this));
}
},
//the following will be run when everything is booted up
ready : function () {
console.log('Attempting to get the previous posts ...');
this.getPosts();
}
});
});
Hasta ahora, el primer métodopostStatus
está funcionando bien
Se supone que el segundo debe llamarse o dispararse directamente en la función ready, sin embargo, no sucede nada. Ni siquiera recibo el mensaje console.logAttempting to get the previous posts ...
. Parece que nunca se dispara.
¿Cual es el problema? ¿Cómo lo soluciono?
Notas: Estoy usando jQuery 3.1.1, Vue.js 2.0.1