Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Meteor to reload subscriptions?

Tags:

meteor

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.

Here's the route:

@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 ]

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.

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?

like image 790
Rijk Avatar asked Sep 29 '14 09:09

Rijk


1 Answers

Turns out this was really simple, and I feel a little stupid now :)

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.

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

And it will pull in the new data. Yay!

like image 192
Rijk Avatar answered Nov 15 '22 10:11

Rijk