简体   繁体   English

在Meteor中没有Collection.find的反应式更新

[英]No Reactive Update with Collection.find in Meteor

In a test app with collections Fruits and Meteor.users , a user clicks on the name of a fruit to add it to his favorite list, using server-side code 在具有FruitsMeteor.users集合的测试应用中,用户使用服务器端代码单击水果的名称以将其添加到他最喜欢的列表中

Meteor.users.update( Meteor.user()._id, { $push: {'profile.favorites': fruit_id } })

where fruit_id is the ObjectID fruit._id generated by Mongo. 其中fruit_id是Mongo生成的ObjectID fruit._id

For the favorite fruits page, the client also subscribes to a publication: 对于最喜欢的水果页面,客户端还订阅出版物:

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

    return Fruits.find({
        '_id': {
            '$in' : Meteor.users.findOne(this.userId).profile.favorites
        }
    })

}

Problem: When a new fruit is made a favorite, nothing changes on the favorite fruits page unless a page refresh is made. 问题:将新水果设为收藏夹时,除非刷新页面,否则收藏夹水果页面上的任何内容都不会更改。

My guess is because in the publication code, the line containing $in is not reactive. 我的猜测是因为在发布代码中,包含$in的行不是反应性的。

For this situation, what is the usual practice to enable the user to reactively see the newly added or removed fruits? 对于这种情况,通常的做法是使用户能够看到新添加或删除的水果吗? Can Meteor.users.findOne(this.userId).profile.favorites be made reactive? Meteor.users.findOne(this.userId).profile.favorites可以设为反应式吗?


The subscription is done in the controller, I'm using Angular with Meteor. 订阅是在控制器中完成的,我在Angular和Meteor中使用。

angular.module('myApp').controller('FavoriteFruitsCtrl', function($scope, $meteor) {

    $meteor.autorun($scope, function() {

        $meteor
            .subscribe('favoriteFruits')
            .then(function() {
                $scope.favfruits = $meteor.collection(Fruits, false)
            })

    })

})

Based on the suggestions by @SylvainB and @Billybobbonnet, are we trying to do this? 根据@SylvainB和@Billybobbonnet的建议,我们是否要这样做? The second autorun containing .subscribe does re-run whenever there is changes to Meteor.user().profile.favorites ! 每当Meteor.user().profile.favorites发生更改时,包含.subscribe的第二个自动运行都会重新运行!

angular.module('myApp').controller('FavoriteFruitsCtrl', function($scope, $meteor) {

    $meteor.autorun($scope, function() {
        Session.set('favorites', Meteor.user().profile.favorites)
    })

    // This autorun block will be triggered when the above autorun is triggered
    $meteor.autorun($scope, function() {
        var justForTriggering = Session.get('favorites')
        $meteor
            .subscribe('favoriteFruits')
            .then(function() {
                $scope.favfruits = $meteor.collection(Fruits, false)
            })
    })

})

However $meteor.subscribe function is not fired (by checking on the server side) 但是,不会触发$meteor.subscribe函数(通过在服务器端进行检查)

I would create a template level subscription using the array of favorite fruits as argument. 我将使用喜欢的水果数组作为参数来创建模板级别的订阅 It assumes that you exposes the profile.favorite field in another publication, but it should already be exposed by default for the current user. 它假定您在另一个出版物中公开了profile.favorite字段,但是默认情况下,当前用户应该已经公开了它。

Since you will wrap your subscription in an autorun and use a cursor from the users collection, the cahin of events will be as follows: updating profile > triggering autorun > updating publication of favorite fruits. 由于您会将订阅包装在自动运行中,并使用用户集合中的光标,因此事件的原因如下:更新配置文件>触发自动运行>更新喜欢的水果的发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM