3

我想知道如何访问 {{#each items}} 循环中的前一个元素。例如 Template.index.messages 返回带有名称和消息的对象数组:

{{#each messages}}
  {{#if previousMessage.name != this.name }}
    {{name}}
  {{/if}}
  {{this.message}} 
{{/each}}

基本上我想在连续的消息中隐藏名字。我通过过滤 Template.index.messages 中的消息来解决此问题,但是每次数据更改时都会调用该方法,因此会消耗大量资源。那么,如何在空格键中做到这一点?

提前致谢。

4

4 回答 4

1

当他们接受我的拉取请求时, 您可以使用https://github.com/raix/Meteor-handlebar-helpers https://github.com/raix/Meteor-handlebar-helpers/pull/65

否则您可以将其添加到您的项目助手中以模拟 $mapped 行为

UI.registerHelper("$mapped", function(arr, extended) {
    var extended = extended || false;
    if(!Array.isArray(arr)){
      try {
        arr = arr.fetch()
      }
      catch (e){
        console.log("Error in $mapped: perhaps you aren't sending in a collection or array.")
        return [];
      }
    }

    var $length = arr.length;

    var mappedArray = arr.map(function(item,index) {
      var newItem = _.clone(item);
      newItem.$length = $length;
      newItem.$index = index;
      newItem.$first = index === 0;
      newItem.$last  = index === $length-1;
      newItem.$index = index;
      if (extended) {
        newItem.$nextEl = arr[index+1];
        newItem.$prevEl = arr[index-1];
        newItem.$firstEl = arr[0];
        newItem.$lastEl = arr[$length-1];
      }
      return newItem;
    });

    return mappedArray || [];
});

用法

{{#each $mapped yourCursor true}}
//Getting the next & previous element 
{{$nextEl._id}}
{{$prevEl._id}}
{{/each}}
于 2016-03-17T10:23:40.033 回答
0

Spacebars 模板语言并不是真正为复杂的逻辑而构建的,所以我不会尝试直接在模板本身中执行此操作。无论您在何处实现名称隐藏代码,它都将使用资源——因此,像您现在这样在帮助函数中执行此操作可能是最简单/最好的方法。作为旁注,spacebars不处理等式语句!=,您必须为此创建一个辅助函数。

如果您真的关心计算时间,减少它的唯一方法是在数据库中存储更多数据(例如最后一条消息的名称)。如果您存储它(例如,在 中.previousName),它会很简单:

在模板中:

{{#each messages}}
    {{#if notEqual previousName name}}
        {{name}}
    {{/if}}
    {{message}}
{{/each}}

在 JavaScript 中:

Template.index.helpers({
    notEqual: function (string1, string2) {
        return string1 !== string2;
    }
});
于 2015-04-29T18:18:41.607 回答
0

我将提供一个不同的答案,这不涉及将更多信息放入数据库中。这可能有点毛茸茸的原因是,如果由于某种原因它所指的“以前的名称”不再存在,那么您的模板逻辑将被关闭。然后,您必须确保在删除(或禁用)数据库中的该记录时,更改引用它的所有记录的“以前的名称”字段。这可能是一种痛苦。这是我的建议,不需要额外的包或数据。

您可以使用 访问模板中任何数组的索引@index

因此,您可以在模板中执行此操作:

{{#each messages}}
  {{#if isSameName @index}}
    {{name}}
  {{/if}}
  {{this.message}} 
{{/each}}

然后在你的助手中:

Template.index.helpers({
    isSameName: (idx) => {
        // Get the messages from your local storage
        let msgs = Messages.find().fetch();
        // Lets make sure we don't get any errors from the previous message not existing
        return (msgs && msg[idx-1] && msgs[idx-1].name === msgs[idx].name);
    }
});

不需要额外的数据库存储或遍历。

于 2016-05-10T14:34:29.020 回答
0

您可以使用下面的代码来获取外部集合。

假设您有名为Collection.CustomerCollection.RechargePlan的集合,并且您在模板中使用这两者来更新客户。

Customer = {"name":"James", "rechargeplan":"monthly"};
RechargePlan = [{"rechargeplan": "monthly"},{"rechargeplan": "yearly"}];

 //Inside template, Bydefault Customer is available.
{{#each RechargePlan}}
  {{#if equals ../rechargeplan rechargeplan}}
      //Hurray Plan matches
  {{/if}}
{{/each}}

在上面的代码中,../rechargeplan实际上是Customer.rechargeplan, ../ 实际上比层次结构高了一步,然后访问该字段(如果可用),因为 Customer 已经可用于模板,所以它的字段被拾取。同样,如果Customer上面有集合,你可以使用 ../../rechargeplan

于 2016-05-14T12:29:50.980 回答