如何强制 Meteor 重新加载订阅? [英] How to force Meteor to reload subscriptions?
问题描述
在我的应用程序中,sections
是通过名为 course.sectionIds
的属性链接到 courses
的集合.初始加载工作正常,但在管理面板中添加部分时遇到了非反应性加入问题.
In my application, sections
is a collection linked to courses
by a property called course.sectionIds
. The initial load works fine, but I am running into a non-reactive join problem when adding a section in the admin panel.
路线如下:
@route 'adminCourse',
path: 'admin/course/:course'
waitOn: -> Meteor.subscribe 'course', @params.course
data: -> Course.first()
并且这些部分包含在课程出版物中:
And the sections are included in the course publication:
Meteor.publish 'course', ( courseId ) ->
return null if not this.userId
# [some permission checks]
courses = Course.find courseId
sections = Section.find _id: $in: _.flatten courses.map ( course ) -> course.sectionIds
[ courses, sections ]
我知道反应式连接,但我不能真正使用方法 #1 或 #4(在客户端上过度发布和加入),因为涉及权限检查(您应该只能看到自己课程的部分).此外,我知道数据何时发生变化,因此它实际上不必是被动的.
I know about reactive joins, but I can't really use approach #1 or #4 (overpublishing and joining on the client), as there are permission checks involved (you should only be able to see the sections of your own courses). Also, I know when the data changes, so it doesn't really have to be reactive.
我只想让 Meteor 知道在用户提交表单以添加新部分时重新加载数据(我目前正在通过执行 window.location.reload()
添加部分后).有没有办法用 Meteor 做到这一点?
I just want to let Meteor know to reload the data, when the user submits the form for adding a new section (I am currently working around this by doing a window.location.reload()
after a section has been added). Is there a way to do that with Meteor?
推荐答案
原来这真的很简单,现在我觉得有点傻:)
Turns out this was really simple, and I feel a little stupid now :)
您只需再次调用 Meteor.subscribe()
即可重新加载订阅.我在使用不同的解决方案时发现了这一点,使用铁路由器导航到不同的 URL 使其重新加载订阅.
You can reload the subscription by simply calling Meteor.subscribe()
again. I found this out while messing with a different solution, using iron router to navigate to a different URL making it reload the subscription.
因此,在我的提交侦听器中,您可以简单地执行以下操作,而不是执行 window.reload()
:
So, in my submit listener, instead of doing window.reload()
, you can simply do the following:
Template.sectionForm.events
'submit form': ( e ) ->
e.preventDefault()
data = SimpleForm.processForm( event.target )
section = Section.create( data )
this.course.push( sectionIds: section._id )
# Reload the subscription to pull in the new section
params = Router.current().params
Meteor.subscribe 'course', params.producer, params.course
它会拉入新数据.耶!
这篇关于如何强制 Meteor 重新加载订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!