Hello there,
I implemented an event calendar with periodic events(like every week, every fourth day, last monday in month…)
Therefore I created a collection with the following fields:
events:{
title:String/sometitle
date:Date//the events date
period:String//sth like "2,w" for every two weeks or "3,m" for every 3. month
}
So now my approach is:
- publishing all events
- generate an Array which contains the constructed events for the next x days
The problem with this approach is, that i have an array now and no collection any more - which forces me to write my own filter and sort functions
Is there a way to somehow fill the clients collection with imaginary items? Then i could still use fitler functions on the collection…
I think this is a kind of awkward approach and if i’m doing this totally wrong a hint on how to do it better would be really nice
/**
* generates an array with events based on the recurring logic in the database for the next 'duration' days
* @param events database events .fetch() array
* @param startdate the startdate to begin calculation
* @type moment() date object
* @param duration the duration in days (default 30)
* @type time in days
*/
var applyRecurringLogic = function(events,startDate,duration){
duration = typeof duration !== 'undefined' ? duration:30;
/**
* is intialized empty and then filled with all the database events and the additional generated events
* @type {Array}
*/
var bloatedArray = [];
/**
* contains the calculated endDate = currentDate + duration
* @type Date
*/
var endDate = moment(startDate).add(duration,'d');
events.forEach(function(doc){
if(doc.isRecurring === true){
/**
* [0]:1-x,[1]:w|m|d
* @type {Array}
*/
var split = doc.frequency.split(",");
/**
* contains the current Documents date
* @type Date
*/
var currentDocumentDate = moment(doc.startDate);
//iterate till event leaves scope
while(endDate >= currentDocumentDate){
//check if event is already in scope
if(currentDocumentDate > startDate){
doc.startDate = currentDocumentDate;
var newObj = JSON.parse(JSON.stringify(doc));
bloatedArray.push(newObj);
}
//iterate to next cycle
currentDocumentDate = currentDocumentDate.add(split[0], split[1]);
}
}else{
bloatedArray.push(doc);
}
});
return bloatedArray;
};
The second problem with this approach is, that i get a lot of “duplicate id” errors.
Regards ,
Lukas