diff --git a/examples/parties/client/client.js b/examples/parties/client/client.js index da20aa3fc5..d0b5e36f16 100644 --- a/examples/parties/client/client.js +++ b/examples/parties/client/client.js @@ -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) { diff --git a/examples/parties/model.js b/examples/parties/model.js index 968a990440..e242a32ea7 100644 --- a/examples/parties/model.js +++ b/examples/parties/model.js @@ -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) {