【问题标题】:How to delay iron router route lookup如何延迟铁路由器路由查找
【发布时间】:2015-11-29 20:05:46
【问题描述】:

考虑以下场景:

有一个名为Resourcescollection,特殊用户可以在admin 站点上对其进行CRUD。

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

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

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

【问题讨论】:

    标签: node.js meteor routing iron-router


    【解决方案1】:

    这样的?您根据资源 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");
        }
      }
    });
    

    【讨论】:

    • 嘿@Curtis。所以想象一下我想创建宁静的端点。例如。如果我作为管理员添加一个名为CarResource,我想动态生成以下路由:GET /carsGET /car/:id/editPUT /car/:id/ 等等。我能够生成订阅接收所有资源的这些路由,因此我可以在页面上舒适地导航,但是从浏览器访问 /car/:id/edit 会给我 404,因为查找发生在订阅终止之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2015-03-28
    • 2014-09-03
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多