spark: apply transform in initial render.

parties example: add displayName using transform instead of keeping it as a
separate function. This allows us to drop a few helpers, too.
This commit is contained in:
David Glasser
2013-03-11 23:07:38 -07:00
parent 26ac481c3f
commit 0d3be81357
4 changed files with 40 additions and 31 deletions

View File

@@ -26,10 +26,10 @@ Template.details.anyParties = function () {
}; };
Template.details.creatorName = function () { Template.details.creatorName = function () {
var owner = Meteor.users.findOne(this.owner); var owner = findOneUser(this.owner);
if (owner._id === Meteor.userId()) if (owner._id === Meteor.userId())
return "me"; return "me";
return displayName(owner); return owner.displayName();
}; };
Template.details.canRemove = function () { Template.details.canRemove = function () {
@@ -71,22 +71,18 @@ Template.details.events({
// Party attendance widget // Party attendance widget
Template.attendance.rsvpName = function () { Template.attendance.rsvpName = function () {
var user = Meteor.users.findOne(this.user); var user = findOneUser(this.user);
return displayName(user); return user.displayName();
}; };
Template.attendance.outstandingInvitations = function () { Template.attendance.outstandingInvitations = function () {
var party = Parties.findOne(this._id); var party = Parties.findOne(this._id);
return Meteor.users.find({$and: [ return findUsers({$and: [
{_id: {$in: party.invited}}, // they're invited {_id: {$in: party.invited}}, // they're invited
{_id: {$nin: _.pluck(party.rsvps, 'user')}} // but haven't RSVP'd {_id: {$nin: _.pluck(party.rsvps, 'user')}} // but haven't RSVP'd
]}); ]});
}; };
Template.attendance.invitationName = function () {
return displayName(this);
};
Template.attendance.rsvpIs = function (what) { Template.attendance.rsvpIs = function (what) {
return this.rsvp === what; return this.rsvp === what;
}; };
@@ -222,7 +218,7 @@ Template.createDialog.events({
}, function (error, party) { }, function (error, party) {
if (! error) { if (! error) {
Session.set("selected", party); Session.set("selected", party);
if (! public && Meteor.users.find().count() > 1) if (! public && findUsers().count() > 1)
openInviteDialog(); openInviteDialog();
} }
}); });
@@ -267,10 +263,6 @@ Template.inviteDialog.uninvited = function () {
var party = Parties.findOne(Session.get("selected")); var party = Parties.findOne(Session.get("selected"));
if (! party) if (! party)
return []; // party hasn't loaded yet return []; // party hasn't loaded yet
return Meteor.users.find({$nor: [{_id: {$in: party.invited}}, return findUsers({$nor: [{_id: {$in: party.invited}},
{_id: party.owner}]}); {_id: party.owner}]});
};
Template.inviteDialog.displayName = function () {
return displayName(this);
}; };

View File

@@ -141,7 +141,7 @@
{{#unless public}} {{#unless public}}
{{#each outstandingInvitations}} {{#each outstandingInvitations}}
<div> <div>
{{invitationName}} {{displayName}}
<span class="label label-inverse pull-right">Invited</span> <span class="label label-inverse pull-right">Invited</span>
</div> </div>
{{/each}} {{/each}}

View File

@@ -150,10 +150,25 @@ Meteor.methods({
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Users // Users
var displayName = function (user) { var userTransform = function (user) {
if (user.profile && user.profile.name) user.displayName = function () {
return user.profile.name; if (this.profile && this.profile.name)
return user.emails[0].address; return this.profile.name;
return this.emails[0].address;
};
return user;
};
findOneUser = function (query, options) {
options = options || {};
options.transform = userTransform;
return Meteor.users.findOne(query, options);
};
findUsers = function (query, options) {
options = options || {};
options.transform = userTransform;
return Meteor.users.find(query, options);
}; };
var contactEmail = function (user) { var contactEmail = function (user) {

View File

@@ -931,6 +931,14 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
_.bind(renderer.annotate, renderer) : _.bind(renderer.annotate, renderer) :
function (html) { return html; }; function (html) { return html; };
// Templates should have access to data and methods added by the transformer,
// but observeChanges doesn't transform, so we have to do it here.
var transformedDoc = function (doc) {
if (cursor.getTransform && cursor.getTransform())
return cursor.getTransform()(EJSON.clone(doc));
return doc;
};
// Render the initial contents. If we have a renderer, create a // Render the initial contents. If we have a renderer, create a
// range around each item as well as around the list, and save them // range around each item as well as around the list, and save them
// off for later. // off for later.
@@ -941,7 +949,7 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
else { else {
itemDict.forEach(function (elt) { itemDict.forEach(function (elt) {
html += maybeAnnotate( html += maybeAnnotate(
itemFunc(elt.doc), itemFunc(transformedDoc(elt.doc)),
Spark._ANNOTATION_LIST_ITEM, Spark._ANNOTATION_LIST_ITEM,
function (range) { function (range) {
elt.liveRange = range; elt.liveRange = range;
@@ -994,11 +1002,7 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
later(function () { later(function () {
var doc = EJSON.clone(fields); var doc = EJSON.clone(fields);
doc._id = id; doc._id = id;
var renderDoc = doc; var frag = Spark.render(_.bind(itemFunc, null, transformedDoc(doc)));
if (cursor.getTransform && cursor.getTransform()) {
renderDoc = cursor.getTransform()(EJSON.clone(renderDoc));
}
var frag = Spark.render(_.bind(itemFunc, null, renderDoc));
DomUtils.wrapFragmentForContainer(frag, outerRange.containerNode()); DomUtils.wrapFragmentForContainer(frag, outerRange.containerNode());
var range = makeRange(Spark._ANNOTATION_LIST_ITEM, frag); var range = makeRange(Spark._ANNOTATION_LIST_ITEM, frag);
@@ -1048,10 +1052,8 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
if (!elt) if (!elt)
throw new Error("Unknown id for changed: " + id); throw new Error("Unknown id for changed: " + id);
applyChanges(elt.doc, fields); applyChanges(elt.doc, fields);
var renderDoc = elt.doc; Spark.renderToRange(elt.liveRange,
if (cursor.getTransform && cursor.getTransform()) _.bind(itemFunc, null, transformedDoc(elt.doc)));
renderDoc = cursor.getTransform()(EJSON.clone(renderDoc));
Spark.renderToRange(elt.liveRange, _.bind(itemFunc, null, renderDoc));
}); });
} }
}); });