【问题标题】:Concatenate results of two queries in a template helper在模板助手中连接两个查询的结果
【发布时间】:2016-03-29 23:39:38
【问题描述】:

我有两个集合:CommandsCommandHistory。我正在尝试制作一个接收它们两者结果的输出,结合起来,CommandHistory 中的任何内容都是粗体。也就是说,看起来像:

**Do the thing**    <--| **Command History*
Doing thing           <-|
thing is in progress  <-| Commands
thing is done         <-|
**Do the other thing**             <-| **Command History**
I don't know what the other thing is <-| Commands

这是一个帮助器的基本开始:

log() {
  const commandCursor = Commands.find({}, { sort: { timestamp: 1 } });
  const commandHistory = CommandHistory.find({}, { sort: { timestamp: 1 } });
  // what now? Need to concatenate them.
  // `.fetch()` makes the interface seem non-reactive.
}

我唯一能真正想到的就是制作另一个 (null) 集合并将两个集合中的所有内容都输出到其中,这似乎有点矫枉过正。

是否可以连接这两个游标的结果,并且仍然让它反应?

【问题讨论】:

  • 是否有将CommandsCommandHistory 相关联的密钥?看起来每个后者都有几个前者。
  • CommandHistory 会在他们提交表单并包含该表单的值时添加。提交成功后,通过 Meteor 方法运行命令
  • 那么这两个集合之间有关键关系吗?
  • 不是真的,一个看起来像{ commands: 'MESSAGE "hello"', timestamp: ISODATE() },一个看起来像{ message: "Hello", timestamp: ISODATE }

标签: javascript meteor meteor-blaze minimongo


【解决方案1】:

我认为您正在寻找的主要是一种组合两个结果集的方法。 为此,您可以使用以下内容:

log: function () {
        var commands = Commands.find({}, { sort: { timestamp: 1 } }).map(function(i){i.origin='command'; return i;});
      var history  = History.find({}, { sort: { timestamp: 1} }).map(function(i){i.origin='history'; return i;});
      var combined = _.union(commands, history);
      return _.sortBy(combined, function(i) { return Number(i.timestamp) });
    }

在这里,我添加了一个名为“origin”的字段来标识每个条目的来源。另外,我正在根据时间戳对组合数据集进行排序,我正在考虑使用一个简单的数字来简化示例。

合并数据集后,您可以使用另一个帮助器打印它们,该帮助器返回带或不带 **,具体取决于来源:

output: function(obj) {
  var text = obj.value || "";
  if(obj.origin == 'history') text = "**"+text+"**";
  return text
}

您可以尝试以下示例代码:

HTML: 你好

<body>
  {{> hello}}
</body>

<template name="hello">
  <ul>
    {{#each log}}
      <li>{{output this}}</li>
    {{/each}}
  </ul>
</template>

JS:

Commands = new Mongo.Collection("commands");
History  = new Mongo.Collection("history");

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  Template.hello.helpers({
    log: function () {
        var commands = Commands.find({}, { sort: { timestamp: 1 } }).map(function(i){i.origin='command'; return i;});
      var history  = History.find({}, { sort: { timestamp: 1} }).map(function(i){i.origin='history'; return i;});
      var combined = _.union(commands, history);
      return _.sortBy(combined, function(i) { return Number(i.timestamp) });
    },

    output: function(obj) {
      var text = obj.value || "";
      if(obj.origin == 'history') text = "**"+text+"**";
      return text
    }
  });

}

if (Meteor.isServer) {
  Meteor.startup(function () {
    Commands.remove({});
    History.remove({});
    for (var i=1; i<5; i++) {
      History.insert({value: 'history '+i, timestamp: 10*i});
      for (var j=1; j<4; j++) 
        Commands.insert({value: 'command '+i+'.'+j, timestamp: 10*i+j});
    }
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 2011-05-16
    • 2013-07-17
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多