Friends, I have the following code:
Selection Object (in the method to obtain is the relevant code):
class Seleccion extends React.Component{
constructor(){
super();
this.state ={
image : null,
selecciones:[]
}
this.obtener();
}
obtener(){
var seleccion = this;
API.consulta({
metodo: 'listas/selecciones',
'callback': function (r) {
console.log("can we proccess?",r);
let selecciones = [];
selecciones.push = r.selecciones;
seleccion.setState({
selecciones:r.selecciones
})
console.log("cambiado 2",seleccion.state);
}
});
}
//más codigo de la clase seleccion
}
API.js
consulta: function (data) {
console.log("llamada desde API");
let parametros = this.default;
console.log("binding this?",data);
let url = this.obtUrl(data.metodo);
if (!data.hasOwnProperty('metodo')) throw Error('No se ha pasado el metodo a consultar');
if (!data.hasOwnProperty('callback')) throw Error('No se ha pasado la funcion callback');
parametros = Object.assign({},this.default,{
dataType: 'jsonp',
url: url
})
console.log("url",parametros.url);
if (data.hasOwnProperty('parametros')) parametros = $.extend(this.default, data.parametros);
if (data.hasOwnProperty('data'))
parametros.data = data.data;
$.ajax(parametros)
.done(data.callback)
.fail(function(){
console.log("something wrong");
})
;
},
At this time, I need to access the "select" object in the callback function to make changes to the state, depending on the response. But if I pass a ".bind (this)" to the query method, it exploits me. How can I do?.
The issue is that with the ajax query I obtain the data of "selections" and depending on the result, I will have to modify the state of the same to render them, that is why I need from the callback function to access to the Selection object. PD : I know that overwriting the variable or renaming it could be accessed, but I understand that with ecma6 there is a new way. Is it like that? thanks.