I have an array containing 7 days (week) and a currentDate. When the currentDate goes below the lowest value of the week array, I want to change the dates of every element in the array and shift them by 1 week.
This is the code I have. The subscribe works fine, but the shifting of 1 week doesn't seem to work at all. What am I doing wrong?
Fiddle here: https://jsfiddle.net/rhsr0m9L/
function CalDay(date, total) {
var self = this;
this.date = ko.observable(date);
this.total = ko.observable(total);
this.dateFormatted = ko.computed(function() {
return this.date().format('ddd Do');
}, this);
}
var TimesheetViewModel = function() {
var self = this;
this.currentDate = ko.observable(moment().startOf('day'));
this.week = ko.observableArray([]);
self.currentDate.subscribe(function () {
if (self.week()[0] != undefined) {
if (self.currentDate() < self.week()[0].date()) {
self.week().forEach(function(d) {
console.log(d.date())
d.date(d.date().add(-7, 'days'))
})
}
}
});
// fill array to start
[0,1,2,3,4,5,6].forEach(function(i) {
self.week.push(new CalDay(self.currentDate().startOf('isoWeek').startOf('day').add(i, 'days'), i));
});
.peek()