【问题标题】:How do I use a subscription in flow-router?如何在流路由器中使用订阅?
【发布时间】:2015-09-30 00:43:47
【问题描述】:
// server/publications.js
Meteor.publish('crewMessages', function(crewId) { 
  return CrewMessages.find({crewId: crewId}, {sort: {submitted: -1}, limit: 100});
});

// lib/router.js
FlowRouter.route('/crew/:crewSlug', {
    subscriptions: function(params) {
        console.log("Subscribed to this crew's chat messages:", params.crewSlug);
        this.register('crewMessages', Meteor.subscribe('crewMessages', params.crewSlug));
    },
    action: function(params) {
      FlowLayout.render("layout", { content: 'crew' });
    }
});

在我的crew.html 模板中:

<div class="container">
  {{> crewChat}}
</div>

还有我的crewChat.html/js

Template.crewChat.helpers({
  messages: function() {
    return CrewMessages.find({}, {sort: {submitted: -1}}); 
  }
});

<div class="ui feed">
  {{#each messages}}
    {{> crewChatMessage}}
  {{/each}}
</div>

在我的crewChat.js 文件中,如何使用我在 Flow-router 中设置的订阅?

【问题讨论】:

    标签: javascript meteor subscription flow-router


    【解决方案1】:

    我在 MeteorPad 为您创建了一个功能示例

    http://meteorpad.com/pad/Ba5DTe94NjFi3ZTPA/Playground_Flow-Router_Chat

    对您的问题最重要的是,您可以在模板中使用 Collection.find() 的集合,因为您只能通过路线订阅订阅船员消息。

    每条消息都可以作为普通文档访问see template showChatMessage

    希望你能明白

    干杯 汤姆

    PS:你也可以更改meteorpad里面的url,也可以切换到team2和team3的聊天

    【讨论】:

    • 很好的回答 Tom,感谢您抽出宝贵时间简化此示例。我现在对 Meteor 中的订阅如何工作有了更多了解。
    猜你喜欢
    • 2015-05-31
    • 2023-03-31
    • 1970-01-01
    • 2016-09-20
    • 2015-05-15
    • 2015-08-27
    • 2015-06-01
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多