【问题标题】:Meteor Subscriptions - Customised Helper Function Not workingMeteor 订阅 - 自定义辅助功能不起作用
【发布时间】:2014-03-17 05:40:21
【问题描述】:

当我在客户端订阅一个 mongodb 集合并在服务器上发布,同时关闭自动发布。我是否需要在发布方法中指定我在辅助函数中声明的每个单独的查找查询?还是只在 Publish 语句中返回 Meteor.collection.find() 就足够了,这应该可以访问整个集合?

迷路了?请看下面

在我的应用程序中,我设置了 2 个 mongo 集合。一个叫“Tables”,另一个叫“Rolls”。

在我的 client.js 文件中,我有两个把手辅助函数:

Template.tables.tableList = function(){
    return Tables.find();   
}

Template.tableBox.table = function(tableID){
    return Rolls.find({"Roll.tableName": tableID}, {sort: {date: -1}, limit:10});
}

对应我的模板:

<template name="tables">
    <div class="table-area">
    {{#each tableList}}
      {{> tableBox}}
    {{/each}}
    </div>
</template>


<template name="tableBox">
    <table id="{{name}}" class="table table-condensed">
        <tr class="n{{minBet}}">
            <th>{{name}}</th>
            <th> Min:</th>
            <th>{{minBet}}</th>
            <th>{{cPlayers}}</th>
        </tr>
        <tr>
            <td>D1</td>
            <td>D2</td>
            <td>D3</td>
            <td>Tot</td>
        </tr>
        {{#each table name}}
          {{> tableRow}}
        {{/each}}
    </table>
</template>


<template name="tableRow">
        <tr class={{rowColor Roll.dice1 Roll.dice2 Roll.dice3 Roll.total}} "bold">
            <td>{{Roll.dice1}}</td>
            <td>{{Roll.dice2}}</td>
            <td>{{Roll.dice3}}</td>
            <td>{{Roll.total}}</td>
        </tr>
</template>

第一个辅助函数返回集合中的所有表。 第二个按降序返回最后 10 个卷。

使用自动发布 - 一切正常。我的页面显示了所有的表格以及每个表格中的最后 10 个骰子。

当我关闭自动发布并尝试设置相应的订阅/发布语句时。它不起作用。仅显示表格 - 但来自 Rolls 集合的数据未填充我的模板。

对应的订阅和发布代码:

在 client.js 中:

Meteor.subscribe('tables');
Meteor.subscribe('rolls');

在 server/publications.js 中:

Meteor.publish('tables', function() {
  return Tables.find();
});

Meteor.publish('rolls', function() {
  return Rolls.find();
});

我的假设是,这与我在车把辅助函数中用于卷表的稍微复杂的查询有关?这不是一个简单的订阅整个集合(即发布 Rolls.find() 的返回)然后能够访问我在客户端中定义的所有 mongo 查询子集吗?我在这里缺少什么吗?感谢您的帮助。

【问题讨论】:

    标签: mongodb meteor publish-subscribe


    【解决方案1】:

    这似乎是由于在您查询时,Rolls 集合尚未完全加载到客户端上。您可以尝试以下方法:

    Template.tables.tableList = function(){
      return Tables.find();   
    }
    
    Template.tableBox.table = function(tableID){
      Deps.autorun(function () {
        return Rolls.find({"Roll.tableName": tableID}, {sort: {date: -1}, limit:10});
      });
    }
    

    每当反应式依赖发生变化时,Deps.autorun(func) 块就会运行封装的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多