与收藏Fruits
和Meteor.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
功能不解雇(由服务器端的检查)
你能告诉我们你的订阅是如何以及在哪里完成的?它基本上归结为使您的订阅反应到'Meteor.user()' – SylvainB
@SylvainB我已经更新了问题的方式订阅目前正在完成 – Nyxynyx