【问题标题】:How Meteor Methods are executed?Meteor 方法是如何执行的?
【发布时间】:2016-01-25 21:55:10
【问题描述】:

关注这个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.');
        });

    }

当用户点击邀请时代码是如何执行的?

同时在客户端和服务器上?

【问题讨论】:

    标签: meteor angular2-meteor


    【解决方案1】:

    假设您的Meteor.methods() 在您的/lib 文件夹中,那么当调用方法时:

    1. 它将首先在客户端执行并异步返回结果,而不会导致任何网络延迟。这是一个称为模拟的过程。
    2. 服务器随后将异步执行相同的代码。
    3. 如果服务器上的结果与客户端上的结果不同,服务器将指示客户端更改其结果以匹配。 (服务器总是获胜)。

    整体效果称为延迟补偿。客户端得到即时结果的错觉(模拟),而服务器则有时间在后台赶上。

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      相关资源
      最近更新 更多