本文介绍了MeteorJS 中的存根方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 MeteorJS 中的存根方法?

What is a stub method in MeteorJS?

为什么包含数据库调用会使其成为非存根?谢谢!

Why does including a database call make it a non-stub? Thanks!

推荐答案

我认为您指的是 docs 中提到的那些?存根是通过 Meteor.methods 定义的.

I think you mean the ones referred to in the docs? The stubs are the ones defined via Meteor.methods.

在 Meteor 中,这些存根允许您进行延迟补偿.这意味着当您使用 Meteor.call 调用这些存根之一时,服务器可能需要一些时间来回复存根的返回值.当您在客户端定义存根时,它允许您在客户端做一些事情来模拟延迟补偿.

In Meteor these stubs allow you to have latency compensation. This means that when you call one of these stubs with Meteor.call it might take some time for the server to reply with the return value of the stub. When you define a stub on the client it allows you to do something on the client side that lets you simulate the latency compensation.

我可以拥有

var MyCollection = new Meteor.collection("mycoll")
if(Meteor.isClient) {
    Meteor.methods({
        test:function() {
            console.log(this.isSimulation) //Will be true
            MyCollection.insert({test:true});
        }
    });
}

if(Meteor.isServer) {
    Meteor.methods({
        test:function() {
            MyCollection.insert({test:true});
        }
    });
}

所以文档会同时插入客户端和服务器.即使服务器没有回复它是否已插入,客户端上的那个也会立即"反映出来.

So documents will be inserted on both the client and the server. The one on the client will be reflected 'instantly' even though the server has not replied with whether it has been inserted or not.

客户端存根允许这种情况发生,即使插入运行两次,也不会插入两个文档.

The client side stub allows this to happen without having two documents inserted even though insert is run twice.

如果插入失败,服务器端一个获胜,服务器响应后客户端自动删除.

If the insert fails, the server side one wins, and after the server responds the client side one will be removed automatically.

这篇关于MeteorJS 中的存根方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:49