node.js - 如何延迟铁路由器路由查找

标签 node.js meteor routing iron-router

考虑以下场景:

有一个 collection称为 Resources特殊用户可以在 admin 上对其进行 CRUD网站。

当普通用户访问网站时,路由会根据对 Resources 的订阅在浏览器中动态创建。 .

我们想暂停路由器查找,直到订阅的回调函数终止。否则,在访问 /<dynamically-created-route> 时会向用户显示“404”直接。

有没有简单的方法来做到这一点?

最佳答案

是这样的吗?您根据资源 ID 创建动态路由,等待具有 id 值的订阅,然后根据您是否可以访问具有该 id 的资源进行路由。这假设您在服务器代码中也有正确的发布设置。

Resources = new Mongo.Collection("resources");

Router.route("/resources/:_id", {
  name: "resources",
  waitOn: function() {
    // Waits on the subscription
    return Meteor.subscribe("resources", this.params._id);
  },
  action: function() {
    // Should only return the resource you subscribed to unless you
    // subscribe to resources in another part of your application
    var resource = Resources.findOne({});
    if (resource) {
      // The resource exists and the user is able to access it.
      this.render("resourceTemplate", {
        data: {
          resource: resource
        }
      });
    }
    else {
      Router.go("/404");
    }
  }
});

关于node.js - 如何延迟铁路由器路由查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384929/

相关文章:

node.js - Nodejs fs 文件处理 : Getting error type in order to handle it more efficient

node.js - app/main.js 部署到 Heroku 时抛出 404?

javascript - 如何获取从客户端到服务器的单程旅行的延迟时间

debugging - meteor 服务器端断点从未在cloud9中命中

android - 你能解释一下 android 中 requestRouteToHost() 的功能吗?

javascript - 使用闭包委托(delegate)运行回调

javascript - meteor 按钮上的切换图标

javascript - MongoDB:如何获取主文档和所有祖先

.net - 如何从 App_Code 中的类使用 Control.GetRouteUrl

google-maps - 使用 Google Maps API 查找最近点