2016-03-09 109 views
0

我几乎是初学者,我正在使用Meteor作为我们在工作中用于UX测试的快速原型。 (用户体验设计师编码,耶)。如何根据流星排序的两个不同模板显示两个不同的模板

现在,这里是问题,我有两个模板集合:CommentsTasks。我想要显示按创建日期排序的这两个组合视图。现在我只能以首秀的意见后,简单地做展示的任务,各自的模板:

<template name="yourTemplate"> 
    {{#if comments.count}} 
     {{#each comments}} 
      {{> comment}} 
     {{/each}} 
    {{else}} 
     <li class="empty"> 
      <h1>No comments yet :(</h1> 
      {{#if currentUser}} 
       <p>Why don't you <span class="write-comment">write one?</span></p> 
      {{else}} 
       <p>Log in to write a new one.</p> 
      {{/if}} 
     </li> 
    {{/if}} 
    {{#each tasks}} 
     {{> task}} 
    {{/each}} 
</template> 

有没有办法简单地“统一”的观点?我正在客户端上做所有事情,没有任何服务器端的东西,也没有安全性,因为它全部在测试系统上本地完成,主持人坐在测试主题旁边,所以我将它放在了不安全的位置,然后自动发布,原型。

我想一种方法是将评论和任务放入一个数组中,并在显示之前将它排序在那里,但这仍然是被动的并且工作吗?我也失去了我不得不说的语法。

在此先感谢您的帮助。

回答

1

正如你所提到的,你可以写一个帮手,将两者结合起来。由于它是帮助程序,因此如果您查询该帮助程序中的集合(或任何反应性数据源),它将是被动的。

Template.yourTemplate.helpers({ 
    items: function() { 
     // get comments and tasks - Add appropriate query properties to filter results 
     var comments = Comments.find({}).fetch(); 
     var tasks = Tasks.find({}).fetch(); 

     //Add a property to each comment object to identify whether an item is a comment or task 
     comments = _.map(comments, function (obj) { 
      obj.isComment = true; 
      return obj; 
     }); 

     //combine comments and tasks into single array 
     var items = comments.concat(tasks); 

     //sort combined array by creation date 
     items = _.sortBy(items, function (item) { 
      return item.creationDate; //edit this based on your requirement 
     }); 
     return items; 
    } 
}); 

然后在你的模板

<template name="yourTemplate"> 
    {{#if items.count}} 
     {{#each items}} 
      <!-- This is the property that we have added in helper --> 
      {{#if this.isComment}} 
       {{> comment}} 
      {{else}} 
       {{> task}} 
      {{/if}} 
     {{/each}} 
    {{else}} 
     <li class="empty"> 
      <h1>No comments and tasks yet :(</h1> 
      {{#if currentUser}} 
       <p>Why don't you <span class="write-comment">write a comment?</span></p> 
      {{else}} 
       <p>Log in to write a new comment.</p> 
      {{/if}} 
     </li> 
    {{/if}} 
</template> 

希望它能帮助。

+0

这工作完美,正是我正在寻找。非常感谢你的帮助,真的很感谢! –

+0

@GüntherFeldbaumer很高兴知道它的工作。你能接受它作为答案吗? :) – Kishor

相关问题