如何从Meteor.method与普通代码中调用Meteor数据库更改器? [英] How does a Meteor database mutator know if it's being called from a Meteor.method vs. normal code?
问题描述
如果某人在客户端上做某事
If one does something on the client like
Foo.insert({blah : 1})
其中Foo
是Meteor集合,实际上会触发 http://docs.meteor.com/#meteor_methods ,该代码可在客户端和服务器上执行代码.
where Foo
is a Meteor collection, this actually triggers a Meteor.method call as documented in the last paragraph of http://docs.meteor.com/#meteor_methods that executes code on both the client and the server.
但是,假设我定义了一个在客户端和服务器上执行相同操作的方法:
However, let's say I define a method (on client and server) that does the same thing:
Meteor.methods({
bar: function() {
Foo.insert({blah : 1})
}
});
现在,我用
Meteor.call("bar");
作为方法的结果,现在将在客户端和服务器上同时调用
Foo.insert
.但是,insert
的客户端调用如何知道不再调用服务器,因为它本身就是方法?
Foo.insert
will now be called both on the client and the server as a result of the method. However, how does the client-side invocation of insert
know not to call the server again, since it is itself a method?
此外,有没有办法在客户端无的情况下调用insert
,而不会自动触发规范的服务器端延迟补偿调用并产生同步? (有关为何要执行此操作的原因,请参见使用伪造的即弃数据加载Meteor客户端应用)
Moreover, is there a way to call insert
on the client side without automatically triggering the canonical server-side latency-compensation call and resulting synchronization? (For why I'd want to do this, see Loading a Meteor client app with fake fire-and-forget data)
推荐答案
客户端一成为存根".它具有此isSimulation
属性设置,使其看起来好像为延迟补偿插入了数据.
The client side one becomes a 'stub'. It has this isSimulation
property set that makes it appear as if the data is inserted for latency compensation.
我认为,如果在客户端上运行.method
,则始终启用延迟补偿,并且不应将其输入数据库.因此,在客户端方法上运行的任何东西都将是伪"类型的模拟
I think if a .method
is run on the client the latency compensation is always enabled and it shouldn't enter in the database. So anything running on a client side method would be a 'fake' type simulation
如果您尝试将this.isSimulation
设置为false
,则会收到一些奇怪的错误,表明您的客户端插入内容开始对插入内容引发错误.
If you try setting this.isSimulation
to false
you'll get some weird error that shows you that the client side insert starts to throw errors with the insert.
我不太确定如何在错误的模拟中运行它.我认为您必须使用其他方法var dothis = function() {...}
类型的方法来运行它.
I'm not too sure how to run it in a false simulation. I think you'd have to run it off in some other method var dothis = function() {...}
type method.
要进行这种假冒"行为,忘记并且只有客户端数据,这就是我所假定的(如果我错了,请更正,以便更改答案),然后在您的客户端方法中修改_collection
属性.
To do this 'fake fire' & forget and have client side only data, which is what I presume (please correct if I'm wrong so I change the answer) modify the _collection
property in your client side method.
例如,如果有
mydata = new Meteor.Collection("something");
function something() {
mydata.insert({..});
}
修改方法来代替
mydata._collection.insert({..});
这将确保数据未同步到服务器,但将使本地集合具有此伪数据".
This will make sure that the data isn't synchronized to the server, yet will have the local collection have this 'fake data'.
希望这会有所帮助!
这篇关于如何从Meteor.method与普通代码中调用Meteor数据库更改器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!