gpt4 book ai didi

javascript - 使用 Meteor 按日期分组

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:55 25 4
gpt4 key购买 nike

我想使用 Meteor 按日期对帖子进行分组,并且仍然保留其特有的 react 性。我不知道这真的有可能。

我正在按照 Discover Meteor 一书中的说明开发一个基于 Microscope 的网站,但我很难进行小的更改,因为我没有使用 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( meteor 问题 644):Are "group by" aggregation queries possible in Meteor, yet?

GitHub Arunoda 的方法:https://github.com/arunoda/meteor-smart-collections/issues/47

此外,我尝试使用 list-grouper(大气包),但无法在我的代码中实现包的说明。

如果这里有好心人知道如何做到这一点,我将不胜感激。非常非常! :)

最佳答案

我没有在 meteor 文档对象上找到“submittedDate”属性。当您向数据库中插入新文档(条目)时,可能需要将当前日期/时间指定为与其余参数一起使用的参数之一。

关于javascript - 使用 Meteor 按日期分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com