I want to store dates taken from a GET request into the array vm.dates. For doing this I use the function push(), later, I want to check whether the date is contained in the array or not. The problem is that when I exit the $http function, I am not able to access the data contained in vm.dates.
I performed the following tests: when I print the array, I read all the dates stored, but when I check the length it returns 0 and if I iterate on it or I want to access an element at index x, it returns undefined. Why this problem? I looked through various questions on Stack Overflow and internet, but no answer yet.
Attached is the portion of code involved.
.controller('PlansController', function($http, $log) {
var vm = this;
vm.events = [];
vm.dates = [];
var req = {
method: 'GET',
url: '***',
headers: {
'Content-Type': "application/json"
}
}
$http(req)
.then(function(response) {
var workouts = response.data.workouts;
var headers = response.headers();
for (var num in workouts) {
var duration = Math.round(workouts[num].duration / 60, 0);
var startingDate = new Date(workouts[num].date);
vm.events.push({
title: workouts[num].muscle_group + "\n" + duration + " min",
start: startingDate,
allDay: true
});
vm.dates.push(startingDate.valueOf());
}
});
/* config object */
vm.uiConfig = {
dayRender: function(date, cell) {
function elementInArray(el, arr) {
for (var ind in arr) {
var intInd = parseInt(arr[ind], 10)
var intEl = parseInt(el, 10)
if (intInd == intEl) {
return true;
}
}
return false;
}
var today = new Date();
var dateRendered = date._d;
if (cell.hasClass('fc-other-month')) {
cell.css("background-color", "#fff");
if (elementInArray(dateRendered.valueOf(), vm.dates)) {
cell.css("background-color", "#000");
}
} else if (cell.hasClass('fc-today')) {
cell.css("background-color", "#fff");
if (elementInArray(dateRendered.valueOf(), vm.dates)) {
cell.css("background-color", "#000");
}
} else {
cell.css("background-color", "#fff");
//$log.info(typeof(vm.dates[1]));
if (elementInArray(dateRendered.valueOf(), vm.dates)) {
cell.css("background-color", "#000");
}
}
}
}
};
/* Event sources array */
vm.eventSources = [vm.events];
});