Created
October 28, 2015 21:21
-
-
Save sakulstra/90610b13246651b8536d to your computer and use it in GitHub Desktop.
calendar javscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template name="events"> | |
{{#each events}} | |
{{title}}-{{date}}<br /> | |
{{/each}} | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The Problem is, that i only get one event with one date | |
Template.events.helpers({ | |
'events':function(){ | |
//get all events | |
var evts = Events.find().fetch(); | |
var newArray = []; | |
currentDate = moment(); | |
//generate Events for the next 30 days | |
calculatedEndDate = moment().add(30,'d'); | |
//iterate over events | |
evts.forEach(function(doc){ | |
//recurring event logic | |
if(doc.recurring === true){ | |
split = doc.scheduling.split(",");//slit "1,w" to "1" and "w" | |
docDate = moment(doc.date);//initialize with "date" in database | |
endDate = doc.endDate? moment(doc.endDate) : calculatedEndDate;//generate for next 30 days or the endDate field | |
while(docDate < endDate){ | |
if(docDate > currentDate){ | |
doc.date = docDate;//modify object | |
var newObj = JSON.parse(JSON.stringify(doc));//clone object | |
newArray.push(newObj);//save new object | |
} | |
docDate = docDate.add(split[0],split[1]);//next iteration step for date | |
} | |
} | |
//non recurring event | |
else{ | |
newArray.push(doc); | |
} | |
}); | |
//sort array by date | |
newArray.sort(function(a,b){ | |
return moment(a.date)-moment(b.date); | |
}); | |
return newArray; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Meteor.publish('events', function(id){ | |
var selector = { | |
appId: id, | |
$or: [ | |
{recurring: true},//recurring events(TODO:check for endDate) | |
{date:{//events in the future | |
$gte:new Date() | |
} | |
} | |
] | |
}; | |
return Events.find(selector); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
title:{ | |
index: true, | |
type:String | |
} | |
date:{ | |
type:Date | |
}, | |
recurring:{//true, when recurring | |
type:Boolean | |
} | |
endDate:{//only repeat events till this date | |
type:Date, | |
optional:true | |
}, | |
format:{//some format string like "2,w" or "4,d" | |
type:String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment