What is a stub method in MeteorJS?

后端 未结 2 419
你的背包
你的背包 2020-12-24 15:17

What is a stub method in MeteorJS?

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

相关标签:
2条回答
  • 2020-12-24 16:00

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

    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.

    I.e I can have

    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.

    0 讨论(0)
  • 2020-12-24 16:11

    For the above code you can write this that will be ran on both server and client, use isSimulation to identify on wich side you are if you need to do specific task :

    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题