【发布时间】:2014-08-16 06:18:49
【问题描述】:
我想使用 Meteor 按日期对帖子进行分组,并且仍然保留其特有的反应性。我不知道这真的可能。
我正在按照“发现流星”一书中的说明开发基于显微镜的网站,但我很难做出小的改变,因为我没有使用流星的经验。
我对原始密码书进行了小幅调整,但没有真正改变其原始结构。
我需要做的是按日期对帖子进行分组,使它们看起来像这样:
-
今天
- 发布 7
- 发布 6
- 发布 5
-
昨天
- 发布 4
- 发布 3
-
2014 年 8 月 11 日
- 发布 2
- 发布 1
我目前的代码结构如下:
/client/view/posts/posts_list.js
Template.postsList.helpers({
posts: function() {
return Posts.find({}, {sort: {submittedDate: -1}});
}
});
/client/view/posts/posts_list.html
<template name="postsList">
<div class="posts">
{{#each posts}}
{{> postItem}}
{{/each}}
{{#if nextPath}}
<a class="load-more" href="{{nextPath}}">Show more</a>
{{/if}}
</div>
</template>
/client/view/posts/post_item.js
Template.postItem.helpers({
ownPost: function () {
return this.userId == Meteor.userId();
},
});
/client/view/posts/post_item.html
<template name="postItem">
<div class="post">
<div class="post-content">
<h3><a href="{{url}}">{{title}}</a><span>{{description}}</span></h3>
</div>
<div class="post-comments">
<a href="{{pathFor 'postPage'}}">{{commentsCount}} comments</a>
</div>
{{#if ownPost}}
<a href="{{pathFor 'postEdit'}}">Edit</a>
{{/if}}
</div>
</template>
/collections/posts.js
Posts = new Meteor.Collection('posts');
Posts.allow({
update: ownsDocument,
remove: ownsDocument
});
Meteor.methods({
post: function(postAttributes) {
var user = Meteor.user(), postWithSameLink = Posts.findOne({url: postAttributes.url});
if(!user)
throw new Meteor.Error(401, "You need to be a registered user to do this");
if(!postAttributes.title)
throw new Meteor.Error(422, "Please, fill the name field");
if(!postAttributes.description)
throw new Meteor.Error(422, "Please, fill the description field");
if(!postAttributes.url)
throw new Meteor.Error(422, "Please, fill the URL field");
if(postAttributes.url && postWithSameLink) {
throw new Meteor.Error(302, "This URL already exist", postWithSameLink._id);
}
var post = _.extend(_.pick(postAttributes, 'url', 'title', 'description'), {
userId: user._id,
author: user.username,
submittedDate: new Date().getTime(),
commentsCount: 0
});
var postId = Posts.insert(post);
return postId;
}
});
/server/publications.js
Meteor.publish('posts', function(options) {
return Posts.find({}, options);
});
Meteor.publish('singlePost', function(id) {
return id && Posts.find(id);
});
Meteor.publish('comments', function(postId) {
return Comments.find({postId: postId});
});
Meteor.publish('notifications', function() {
return Notifications.find({userId: this.userId});
});
我尝试了几个在这里和 GitHub 上找到的解决方案,但我无法让它们中的任何一个工作。我尝试过的解决方案是:
StackOverflow 1(流星问题 644):Are "group by" aggregation queries possible in Meteor, yet?
GitHub Arunoda 的方法:https://github.com/arunoda/meteor-smart-collections/issues/47
另外,我尝试使用 list-grouper(atmosphere 包),但无法在我的代码中实现包的说明。
如果这里有任何好心人知道如何做到这一点,我将不胜感激。非常非常非常! :)
【问题讨论】:
-
嗨!我编写了 list-grouper 包,可能可以向您展示如何使用它,但我目前不在电脑前。我会回复你的。
-
好的。谢谢@KristofferK。我会很感激的! :)
标签: javascript meteor