流星,一对多关系&在发布中仅向客户端集合添加字段? [英] Meteor, One to Many Relationship & add field only to client side collection in Publish?

查看:91
本文介绍了流星,一对多关系&在发布中仅向客户端集合添加字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以看到此代码中可能出现的问题,基本上我想检查当前登录用户是否共享了帖子并将一个临时字段添加到客户端集合中:isCurrentUserShared。

Can anyone see what may be wrong in this code, basically I want to check if a post has been shared by the current logged in user AND add a temporary field to the client side collection: isCurrentUserShared.

这是第一次加载新页面并从现有共享填充时,或者在加载页面时第一次添加或删除记录到Shares集合时。

This works the 1st time when loading a new page and populating from existing Shares, or when adding OR removing a record to the Shares collection ONLY the very 1st time once the page is loaded.

1)isSharedByMe只更改状态1次,然后仍按照console.log调用回调,但isSharedByMe在第1次后没有在Posts集合中更新我添加或删除记录。它第一次工作。

1) isSharedByMe only changes state 1 time, then the callbacks still get called as per console.log, but isSharedByMe doesn't get updated in Posts collection after the 1st time I add or remove a record. It works the 1st time.

2)为什么连续两次调用回调,即将一条记录添加到Sharescollection触发2次调用,如console.log所示。

2) Why do the callbacks get called twice in a row, i.e. adding 1 record to Sharescollection triggers 2 calls, as show by console.log.

Meteor.publish('posts', function() {

    var self = this;
    var mySharedHandle;


    function checkSharedBy(IN_postId) {
        mySharedHandle = Shares.find( { postId: IN_postId, userId: self.userId }).observeChanges({

            added: function(id) {
                console.log("   ...INSIDE checkSharedBy(); ADDED: IN_postId = " + IN_postId );
                self.added('posts', IN_postId, { isSharedByMe: true });
            },

            removed: function(id) {
                console.log("   ...INSIDE checkSharedBy(); REMOVED: IN_postId = " + IN_postId );
                self.changed('posts', IN_postId, { isSharedByMe: false });
            }
        });
    }


    var handle = Posts.find().observeChanges({

        added: function(id, fields) {
            checkSharedBy(id);
            self.added('posts', id, fields);
        },

        // This callback never gets run, even when checkSharedBy() changes field isSharedByMe.
        changed: function(id, fields) {
            self.changed('posts', id, fields);
        },

        removed: function(id) {
            self.removed('posts', id);
        }
    });

    // Stop observing cursor when client unsubscribes
    self.onStop(function() {
        handle.stop();
        mySharedHandle.stop();
    });

    self.ready();
});


推荐答案

我个人认为这是一个非常不同的通过使用$ in运算符,并在记录中保留一个postIds或shareIds数组。

Personally, I'd go about this a very different way, by using the $in operator, and keeping an array of postIds or shareIds in the records.

http://docs.mongodb.org/manual/reference/operator/query/in/

我发现发布函数在保持简单时效果最好,如下所示。

I find publish functions work the best when they're kept simple, like the following.

Meteor.publish('posts', function() {
    return Posts.find();
});
Meteor.publish('sharedPosts', function(postId) {
    var postRecord = Posts.findOne({_id: postId});
    return Shares.find{{_id: $in: postRecord.shares_array });
});

这篇关于流星,一对多关系&在发布中仅向客户端集合添加字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆