Based on three function - GET, TRANSFORM and CHANGECHART - how can I chain all three through callbacks? Bearing in mind, that CHANGECHART can be run as a standalone function, when the GET and TRANSFORM are done?
Here is my code:
var dts = {};
var chart = {};
function testAjax(callback) {
$.ajax({
url:
"https://api.openweathermap.org/data/2.5/forecast?q=london&appid=7ce3e1102e1902e0f878c2a640e95aed",
method: "GET",
cache: false,
success: callback
});
}
function transform(data) {
this.data = data;
dts = data.list.map(item => item.dt);
console.log(dts);
}
function changeChart(text, dts) {
chart.name = text;
chart.data = dts;
console.log(chart.data);
};
$('.change').on('click', function() {
var text = $(this).html();
changeChart(text, dts);
})
$('.submit').on('click', function() {
testAjax(transform);
})
console.log(useMe.tempMins); // undefined
Expected results: on click of changeChart, dts variable IS DEFINIED and replaces chart.data object data.
More on codepen: https://codepen.io/anon/pen/zWprar?editors=1011
EDIT: I fixed my code to the point, where I can properly ask for the answer. The problem lies with changeChart function. How can I pass ajax data to it? Please bear in mind, that the logic for the user is to click submit first to fetch the data, followed by change. Also, in the bigger picture there are multiple 'change' buttons that will change bits and pieces from the main data fetched from ajax request.