简体   繁体   English

在Meteor.js中基于URL发布集合

[英]Publish Collections Based on URL in Meteor.js

Pseudocode: 伪代码:

Meteor.publish 'stuff', ->
    if this.userId
        return doStuffForLoggedInUsers(this.userId)
    else if url matches '/some_url/:user_api_key'
        return doStuffForApiKey(apiKey)
    else
        return null

A solution using Iron Router would be optimal, but a non routing framework solution would also help. 使用Iron Router的解决方案将是最佳的,但非路由框架解决方案也会有所帮助。

Perhaps something like this: 也许是这样的:

client 客户

Router.map(function() {
  this.route('postsWithKey', {
    path: '/posts/:apiKey',
    template: 'posts',
    before: function() {
      this.subscribe('posts', this.params.apiKey);
    }
  });

  return this.route('posts', {
    before: function() {
      this.subscribe('posts');
    }
  });
});

server 服务器

Meteor.publish('posts', function(apiKey) {
  check(apiKey, Match.Optional(String));

  if (apiKey) {
    return Posts.find({key: apiKey});
  } else if (this.userId) {
    return Posts.find({owner: this.userId});
  }
});

When the route with an api key is run, the client will activate the posts subscription with the key. 当运行带有api密钥的路由时,客户端将使用密钥激活posts订阅。 On the server, one cursor is returned if the key exists, otherwise a different cursor is returned if the user is logged in. You could do more sophisticated validation of the key - eg throw an error if it doesn't exist in the database. 在服务器上,如果密钥存在,则返回一个游标,否则如果用户登录则返回不同的游标。您可以对密钥进行更复杂的验证 - 例如,如果数据库中不存在错误,则抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM