Apologies for any language barriers in my communication. I am currently working on developing a page for reporting current work progress. As part of this task, I have created a function called getWeek() to obtain the current week of the month and store user input in a Parse database. However, I am encountering an issue with updating the $scope variable while fetching data from the Parse JS SDK. Any assistance provided would be greatly appreciated. Please find the code snippet below.
angular.module('workReport', ["xeditable"])
.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
})
.factory('Reports', function($q) {
return {
getData: function() {
var defered = $q.defer();
var data = getWeek();
var query = new Parse.Query('workReport');
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length + " scores.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
for (var j = 0; j < data.length; j++) {
if (data[j].date.getTime() == object.get('date')) {
data[j].activity = object.get('activity');
data[j].mrngSession = object.get('mrngSession');
data[j].afterSession = object.get('afterSession');
data[j].completed = object.get('completed');
}
}
}
defered.resolve(data);
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
console.log(defered.promise)
return defered.promise;
}
}
})
.controller('WeekReportCtrl', function($scope, Reports) {
$scope.week = Reports.getData().then(function(r) {
return r;
}, function(r) { alert('Failed ' + r) })
});
function getWeek() {
var d = new Date
d = d.toString().split(" ");
var day = d[0];
switch (day) {
case 'Mon':
return weeks(1, 1, 5);
break;
case 'Tue':
return weeks(2, -1, 4);
break;
case 'Wed':
return weeks(3, -2, 2);
break;
case 'Thu':
return weeks(4, -3, 1);
break;
case 'Fri':
return weeks(5, -4, 0);
break;
default:
console.log('Nothing');
break;
}
function weeks(d, s, e) {
var week = [];
for (var i = s; i <= e; i++) {
var j = new Date;
j.setHours(0, 0, 0, 0)
var today = false
if (i == 0) {
today = true
}
j.setDate(j.getDate() + i);
week.push({
date: j,
activity: "",
mrngSession: "",
afterSession: "",
completed: 0,
today: today
});
};
return week;
}
}