问题
I want to have the ability in my application for a user to befriend another user. A friend request is sent, and then the other person has the option to accept or not. I want there to be two different lists, the first list being the users friends, and the second being people who sent friend requests to him or her. Currently I have a friend collection:
Friends = new Meteor.Collection("friends");
The way I am creating friends is:
Template.friend.events({
'click #addFriend': function(e, t) {
var email = $("#friendEmail").val();
var friend = Meteor.users.find({ "email.address" : email });
if (friend) {
return Friends.insert({friend1:Meteor.userId(),
friend2:friend._id,
name: email,
stat: 'Pending'});
}
}
});
Now I get a little stuck, I have a template at the moment to retrieve friends:
Template.friend.friends = function () {
return Friends.find({friend1: Meteor.userId()});
}
And then I have in my html:
{{#each friends}}
<div class="row">
<div class="... columns">
<p>{{name}} {{stat}}</p>
<div class="... columns">
<p>{{name}} {{stat}}</p>
</div>
</div>
{{/each}}
to render the friends.
But here is the problem: How do I make sure friend two can accept any requests, and how do I make two separate columns? More importantly, I feel this is an incorrect way of doing things, should I even be approaching it this way?
EDIT:
I have gotten rid of the Friends
collection and made a Notifications
collection following phoenix's protocols, and I have created the following to test and see if it would work:
Template.friends.sent = function() {
return Notifications.find({from: Meteor.user(),
type: "friendship"
});
}
With the following html:
{#each sent}
<p>{{to.emails.0.address}}</p>
{/each}
But it returns an error saying:
Expected IDENTIFIER
回答1:
Keep two collections:
Meteor.users
and Notifications
When a friend request is sent you register it on Notifications
, with type : friendship
, status : pending
, from : user1
and to : user2
.
Then, say in the user profile, you can query for the pending notifications that user2 regardless of from whom they are. If user2 accepts then you register it for user1 on collection Meteor.users
. After the request is processed, either if accepted or not, you can delete the notification.
In fact, I you could even take out the property status
from the collection Notificationts
since in this approach you can end up deleting them anyway.
Regarding the two column template. Are you using Bootstrap or plain css?
With Bootstrap (3 in this case):
<div class="row">
<div class="col-xs-6">{{columnOne}}</div>
<div class="col-xs-6">{{columnTwo}}</div>
</div>
With plain CSS
.row { width: 100%; }
.column { width: 50% }
来源:https://stackoverflow.com/questions/23351391/am-i-approaching-making-a-friend-collection-the-correct-way