Meteor:什么时候 this.isSimulation 比 Meteor.isClient 更有用? [英] Meteor: When is this.isSimulation more useful than Meteor.isClient?
问题描述
this.isSimulation 的定义:
Definition of this.isSimulation:
Access inside a method invocation. Boolean value, true if this invocation is a stub.
Meteor.isClient 的定义:
Definition of Meteor.isClient:
Boolean variable. True if running in client environment.
this.isSimulation 在什么情况下有用?为什么我不能总是只使用 Meteor.isClient?
In what situations would this.isSimulation be useful? Why can't I always just use Meteor.isClient?
我正在使用 Meteor.isClient 来检查客户端文档是否使用这个简单模式属性与服务器同步:
I'm using Meteor.isClient to check if a client document is synced with the server using this simple-schema property:
unsynced: {
type: Boolean,
autoValue: function () {
return Meteor.isClient;
}
}
它似乎有效,但其他人说使用 this.isSimulation,只是在这里这样做对我不起作用.
It seems to be working, but others said use this.isSimulation, except doing that here didn't work for me.
推荐答案
我正在接受 Slava 的评论并将其转化为答案 ;)这个想法是 isSimulation
可以在任何地方,在服务器或客户端上实现.它仅在 Method
上下文中有用.正如 Slava 所说,当一台服务器充当另一台服务器的客户端时,可以使用模拟.
I'm taking Slava's comment and turn it into an answer ;)
The idea is that isSimulation
may become true anywhere, on the server or the client. It is only useful inside the Method
context. Simulation may be used, as Slava said, when a server is acting as the client for another server.
isClient
可用于各种代码,而不仅仅是在 Method
内部.它可以防止代码在可能引发错误的环境中执行(例如在服务器上使用 Session
).
isClient
can be used for all kinds of code, not just inside Method
. It prevents code from being executed in an environment where it might else throw an error (like using Session
on the server).
对于延迟补偿,您通常在客户端和服务器上运行相同的方法,但如果您将数据插入数据库,显然客户端只能模拟这一点.因此,有时有助于确定模拟是否正在运行或数据库是否实际更改.
For latency compensation you oftentimes run the same method on the client and on the server, but if you're inserting data into the database obviously the client can only simulate this. As such it is sometimes helpful to determine whether a simulation is running or the database is actually changed.
在您的情况下,您可能需要调整方法以检查服务器是否保存了文档以使其更可靠.为此,我将在方法中添加一个 isServer
块以将标志 synced
设置为 true
并将其设置为 false
> 每当客户更改数据时.最好将它与成功的更新/upsert 结合起来.
In your case you might want to adjust the method to check whether the server saved the document to be more robust. For that I would add an isServer
block into the method to set a flag synced
to true
and set it to false
whenever a client changes data. Ideally couple it with a successful update/upsert.
这篇关于Meteor:什么时候 this.isSimulation 比 Meteor.isClient 更有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!