以下http://www.angular-meteor.com/tutorials/socially/angular2/meteor-methods
拥有:
Meteor.methods({
invite: function(partyId: string, userId: string) {
check(partyId, String);
check(userId, String);
let party = Parties.findOne(partyId);
if (!party)
throw new Meteor.Error('404', 'No such party!');
if (party.public)
throw new Meteor.Error('400', 'That party is public. No need to invite people.');
if (party.owner !== this.userId)
throw new Meteor.Error('403', 'No permissions!');
if (userId !== party.owner && (party.invited || []).indexOf(userId) == -1) {
Parties.update(partyId, { $addToSet: { invited: userId } });
let from = getContactEmail(Meteor.users.findOne(this.userId));
let to = getContactEmail(Meteor.users.findOne(userId));
if (Meteor.isServer && to) {
Email.send({
from: 'noreply@socially.com',
to: to,
replyTo: from || undefined,
subject: 'PARTY: ' + party.name,
text: `Hi, I just invited you to ${party.name} on Socially.
\n\nCome check it out: ${Meteor.absoluteUrl()}\n`
});
}
}
}
})
然后在Party-Detail.ts中我们有
invite(user:Meteor.User) {
this.call('invite', this.party._id, user._id, (error) => {
if (error) {
alert(`Failed to invite due to ${error}`);
return;
}
alert('User successfully invited.');
});
}
当用户点击Invite如何执行代码时?
在客户端和服务器同时?
答案 0 :(得分:0)
假设您的Meteor.methods()
位于/lib
文件夹中,那么当调用某个方法时:
整体效果称为延迟补偿。客户端获得即时结果的幻觉(模拟),同时服务器有时间赶上后台。