从另一个方法存根(客户端代码)调用一个方法(服务器代码)

Calling a method (server code) from another's method stub (client code)

本文关键字:代码 方法 一个 服务器 另一个 客户端 存根 调用      更新时间:2023-09-26

出于性能原因,我想从另一个方法存根(仅客户端代码)调用Meteor方法,但我希望被调用的方法也在服务器端运行

似乎下面的methodBar只在客户端运行。这是故意的吗?有别的办法吗?

const methodFoo = new ValidatedMethod({
  // name, validate not important
  run() { 
    // ...some common code here server&client...
    if (Meteor.isClient) {
      // ...several rather expensive mongo queries, must be client-only...
      if (miniMongoResultsSaySo) Meteor.call('methodBar'); 
    }
  }
});

Meteor在方法中分离客户端和服务器方面似乎真的很固执!

我可以使methodFoo通过Tracker依赖项调用methodBar的意识松散。用火打火,用流星打流星:)

const barNeedsToRun = new Tracker.Dependency;
if (Meteor.isClient) {
  Tracker.autorun((computation) => {
    barNeedsToRun.depend();
    if (!computation.firstRun) methodBar.call();
  }
}

然后在methodFoo中,我将Meteor.call改为barNeedsToRun.changed()