How to get Meteor to reload subscriptions?

In my application, sections is a collection associated with courses using a property called course.sectionIds . The bootstrap works fine, but I am running a non-responsive connection issue when adding a section to the admin panel.

Here's the route:

 @route 'adminCourse', path: 'admin/course/:course' waitOn: -> Meteor.subscribe 'course', @params.course data: -> Course.first() 

And 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 associations , but I can’t use approach # 1 or # 4 (overfulfillment and joining a client), as there are permissions checks (you should only see sections of your own courses). In addition, I know when data changes, so it really shouldn't be reactive.

I just want to tell Meteor to reload the data when the user submits a form to add a new section (I'm currently working on this by doing window.location.reload() after adding the section). Is there a way to do this with Meteor?

+5
source share
3 answers

It turns out that it was very simple, and I feel a little stupid now :)

You can reload the subscription simply by calling Meteor.subscribe() again. I found this by messing with another solution using an iron router to go to a different url to reload the subscription.

So, in my listening view, instead of 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 he will pull new data. Hurrah!

+5
source

For anyone interested, I fixed it by adding observeChanges to my publish function:

 Meteor.publish 'course', ( courseId ) -> return null if not this.userId # [some permission checks] courses = Course.find courseId sectionIds = _.flatten courses.map ( course ) -> course.sectionIds handle = courses.observeChanges changed: ( id, fields ) => if _.has fields, 'sectionIds' addedIds = _.difference fields.sectionIds, sectionIds removedIds = _.difference sectionIds, fields.sectionIds sectionIds = fields.sectionIds _.each addedIds, ( id ) => @added 'sections', id, Section.first id _.each removedIds, ( id ) => @removed 'sections', id @onStop -> handle.stop() sections = Section.find _id: $in: sectionIds [ courses, sections ] 

The observer checks for changes to the sectionIds property, and when this happens, you call either the added or removed methods in the subscription. This makes the compound reactive; when identifiers are added to the courses.sectionIds property courses.sectionIds documents of the new section are automatically transferred to the client.

+7
source

You can simply meteor-related package:

 Meteor.publish 'course', (courseId) -> # verify that courseId is really an ID and not {} or something else return unless @userId @related (course) -> # [some permission checks] Section.find _id: $in: course.sectionIds , Course.find courseId 

I assume that courseId matches only one course. Thus, flatulence only works with one-to-many relationships. In your code above, you have done several things for many.

Another way is that in all these documents (questions, parameters, steps, etc.) you have the main identifier, therefore the identifier of the course. And then you simply filter out all these documents based on the course identifier and publish it for the client, and then you show it on the client in any hierarchy you want. Therefore, instead of trying to calculate the relationship while reading, you calculate the relationship while writing and simply store the identifier of this relationship in all the documents you are interested in.

Disclaimer: I am the author of this package.

+3
source

Source: https://habr.com/ru/post/1203599/


All Articles