parties: use transform to make "attending" a method

This commit is contained in:
David Glasser
2013-03-11 22:42:01 -07:00
parent b27dd1b8d2
commit 26ac481c3f
2 changed files with 12 additions and 9 deletions

View File

@@ -33,7 +33,7 @@ Template.details.creatorName = function () {
};
Template.details.canRemove = function () {
return this.owner === Meteor.userId() && attending(this) === 0;
return this.owner === Meteor.userId() && this.attending() === 0;
};
Template.details.maybeChosen = function (what) {
@@ -131,7 +131,7 @@ Template.map.rendered = function () {
var selected = Session.get('selected');
var selectedParty = selected && Parties.findOne(selected);
var radius = function (party) {
return 10 + Math.sqrt(attending(party)) * 10;
return 10 + Math.sqrt(party.attending()) * 10;
};
// Draw a circle for each party
@@ -158,7 +158,7 @@ Template.map.rendered = function () {
// Label each with the current attendance count
var updateLabels = function (group) {
group.attr("id", function (party) { return party._id; })
.text(function (party) {return attending(party) || '';})
.text(function (party) {return party.attending() || '';})
.attr("x", function (party) { return party.x * 500; })
.attr("y", function (party) { return party.y * 500 + radius(party)/2 })
.style('font-size', function (party) {

View File

@@ -13,7 +13,14 @@
invited: Array of user id's that are invited (only if !public)
rsvps: Array of objects like {user: userId, rsvp: "yes"} (or "no"/"maybe")
*/
Parties = new Meteor.Collection("parties");
Parties = new Meteor.Collection("parties", {
transform: function (p) {
p.attending = function () {
return (_.groupBy(this.rsvps, 'rsvp').yes || []).length;
};
return p;
}});
Parties.allow({
insert: function (userId, party) {
@@ -37,15 +44,11 @@ Parties.allow({
remove: function (userId, parties) {
return ! _.any(parties, function (party) {
// deny if not the owner, or if other people are going
return party.owner !== userId || attending(party) > 0;
return party.owner !== userId || party.attending() > 0;
});
}
});
var attending = function (party) {
return (_.groupBy(party.rsvps, 'rsvp').yes || []).length;
};
Meteor.methods({
// options should include: title, description, x, y, public
createParty: function (options) {