MeteorJS中的存根方法是什么?

nub*_*ela 13 javascript meteor

MeteorJS中的存根方法是什么?

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

Aks*_*hat 28

我认为你的意思是文档中提到的那些?存根是通过定义的存根Meteor.methods.

在Meteor中,这些存根允许您进行延迟补偿.这意味着当您使用Meteor.call它调用其中一个存根时,服务器可能需要一些时间来回复存根的返回值.当您在客户端上定义存根时,它允许您在客户端执行某些操作,以便模拟延迟补偿.

即我可以拥有

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});
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,文档将插入客户端和服务器上.即使服务器没有回复是否已经插入,客户端上的那个将立即反映出来.

客户端存根允许这样做,即使插入运行两次,也不会插入两个文档.

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


Reb*_*lon 7

对于上面的代码,您可以编写将在服务器和客户端上运行的代码,如果您需要执行特定任务,请使用isSimulation来识别您的身份:

var MyCollection = new Meteor.collection("mycoll")
Meteor.methods({
    test:function() {
        console.log(this.isSimulation) //Will be true on client and false on server
        var colItem = {test:true, server: true};
        if (this.isSimulation) {
            colItem.server = false;
        }
        MyCollection.insert(colItem);
    }
});
Run Code Online (Sandbox Code Playgroud)