2015-12-17 53 views
0

与收藏FruitsMeteor.users一个测试应用程序,用户点击一个水果的名称,将其添加到自己的收藏夹列表,使用服务器端代码与Collection.find无功更新流星

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

其中fruit_id是由Mongo生成的ObjectID fruit._id

有关喜爱的水果网页,客户端还签约有出版:

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

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

} 

问题:当一个新的水果制成的最爱,没有最喜欢的水果页面上的变化,除非页面刷新制作。

我的猜测是因为在发布代码中,包含$in的行不是被动的。

对于这种情况,通常的做法是让用户反应性地看到新添加或去除的水果? Meteor.users.findOne(this.userId).profile.favorites可以做出反应吗?


订阅在控制器中完成,我正在使用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) 
      }) 

    }) 

}) 

基于由@SylvainB和@Billybobbonnet的建议,我们试图做到这一点?包含.subscribe的第二个自动运行会在Meteor.user().profile.favorites发生更改时重新运行!

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) 
      }) 
    }) 

}) 

但是$meteor.subscribe功能不解雇(由服务器端的检查)

+0

你能告诉我们你的订阅是如何以及在哪里完成的?它基本上归结为使您的订阅反应到'Meteor.user()' – SylvainB

+0

@SylvainB我已经更新了问题的方式订阅目前正在完成 – Nyxynyx

回答

0

我会用最喜欢的水果作为参数的阵列创建一个template level subscription。它假定您在另一个出版物中公开profile.favorite字段,但它应该已经默认公开给当前用户。

由于您将自动套用您的订阅并使用用户集合中的光标,因此事件的大小如下: 更新配置文件>触发自动运行>更新最喜欢水果的发布。

+0

是的,甚至不需要使用最喜欢的水果数组(这样他或她可以在服务器端保持这种检查,我猜测这是需要的)。只需检查用户何时更新。如果你想更具体,[看这里](http://stackoverflow.com/a/29753793/1439597)。 – SylvainB

+0

@davidweldon的另一个不错的诀窍。谢谢你提到它。 – Billybobbonnet