Iron Router 动态路由到新 collection 文档 url

Iron Router routing to new collection document url dynamically

我有一个目标mongo collection.

我正在使用 Iron Router 通过“/goals/:_id”导航到每个目标文档

这非常适合点击应用程序。

但是,当用户向 collection 添加新目标时(通过服务器插入方法),我想同时自动导航到该页面。

客户端:

'click #new-goal': () ->
    Meteor.call("newGoal", (error, result) ->
        Router.go('/goal/#{result}')
        console.log "#{result} created and user redirected.")

服务器上的 newGoal 方法 运行s 和 Router.go 是回调 returns 之前的 运行。

我应该使用 "waitOn" 还是有更好的实现方式?

谢谢!

检查您的 Coffeescript 缩进。 Router.go 不应该在回调之前发生。


关于 Meteor 文档所说的争论,这里是文档的片段:

When the method is complete (which may or may not happen before Meteor.call returns), the callback will be called with two arguments: error and result. If an error was thrown, then error will be the exception object. Otherwise, error will be undefined and the return value (possibly undefined) will be in result.

这是什么意思?

  1. Meteor.call 之后的代码将 运行 在方法 returns 之前(不包括回调内的代码)。
  2. 回调不会运行直到服务器端方法完成(这是合乎逻辑的,因为在方法完成之前无法提供响应或错误。这也符合观察到的行为。此外,它与实际流星代码一致here