我有一个关于Meteor的一般性问题,它与客户端更新结合使用角度。
让我们假设我正在使用meteors Accounts bundle并使用填充了用户对象(或任何其他集合的“users”集合,这没关系)。现在我想构建一个详细信息页面,我想显示一个用户对象的详细信息。
在此页面上,我订阅了以下出版物:
// server code
Meteor.publish('userDetails', function(userId) {
return Meteor.users.find({ _id: userId });
});
在客户端,我正在订阅并加载这样的对象:
$scope.user = $scope.$meteorObject(Meteor.users, userId, false)
.subscribe('userDetails', userId);
在详细信息页面上是一个触发meteor方法的按钮,该方法更新服务器端当前显示的用户对象。像这样:
模板:
// client template
<div>User object: {{user}}</div>
<button ng-click="modifyUser()">Do it!</button>
控制器:
// client controller
...
$scope.modifyUser = function() {
$meteor.call('updateUser', $scope.user._id, 'foo').then(function(result) {
...
}, function(err) {
...
});
}
服务器方法:
// server
Meteor.methods({
updateUser: function(userId, someValue) {
return Meteor.users.update({ _id: userId }, { $set: { 'profile.someValue': someValue }});
}
});
现在我的问题:
我期待的是在updateUser
客户端方法调用之后自动更新用户对象。但实际上什么也没发生。我做错了什么?
一般情况下:如何触发一般在服务器上修改的模型的客户端更新?如何在使用Meteor-Angular时实现这一目标?
答案 0 :(得分:0)
我找到了问题的原因:我的所有Meteor.methods(...)
都只在服务器端定义。现在一切似乎都按预期工作了。