javascript - 如何在流量路由器中使用订阅?

标签 javascript meteor subscription flow-router

// 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 中设置的订阅?

最佳答案

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

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

对于您的问题最重要的是,您可以通过 Collection.find() 在模板中使用 Collection,因为您仅通过路由订阅订阅了工作人员消息。

每条消息都可以作为普通文档访问请参阅模板 showChatMessage

希望这能让你明白

干杯 汤姆

PS:你也可以更改meteorpad内的url来切换team2和team3的聊天

关于javascript - 如何在流量路由器中使用订阅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31354788/

相关文章:

javascript - Meteor:JS 警报框 'cancel' 正在删除我的数据库条目,而不是取消请求的删除

javascript - 在 meteor 助手中发出 ajax 请求

meteor - meteor Accounts.changePassword的错误格式不一致

c# - 当变量更改时生成事件以从代码类发送以形成类 c#

javascript - 需要一个指令的 Controller 位于另一个指令中

javascript - 设置innerHTML字符串会产生语法错误

Javascript:将对象从一个数组移动到另一个数组:最佳方法?

javascript - AJAX最后运行的功能,如何让它首先运行

iOS 应用内购买和订阅 : Testing

Paypal 订阅错误 : The link you have used to enter the PayPal system is invalid. 请检查链接并重试