0

What is the intended reactive behaviour when adding a document to a published collection? More specific: does adding a document to a collection invalidate all subscriptions to that collection (even those that are not matching) and result in a re-rendering of all dependent ui elements?

That's what I'm experiencing right now, at least.

To be even more specific: I have forms, with some "normal" fields but also lists. Those lists contain subforms which contain fields as well. Both types of forms are stored in a single generic Data collection. The view on this Data collection is managed with fine-grained subscriptions.

When I add a new list element, a new subform to the base form, the whole base form is re-rendered. Even none related base forms are re-rendered.

I have a github repo showing this, a little obfuscated though. It's a movie database. You can add a movie, give it a name, a tagline and add actors to the movie. An actor has a name and a home. When adding an actor to a movie every movie is re-rendered.

https://github.com/Crenshinibon/fields3/tree/462a9291bfc400a2731c21d2debdd4071be764ed

I'm aware of this question: Understanding Meteor Publish / Subscribe and think this part is actually missing. (I just don't have enough reputation points to ask in the comments) I understand the Pub/Sub mechanism of Meteor pretty well, I think. Just the resulting reactive behaviour is a little bit unclear.

I'm also aware of this question: Reactivity, isolation, and lists. But it's for Spark and Blaze changed a lot (especially, it made #isolate obsolete.)

And before I rebuild my app (again) to put all kinds of forms (or even properties) in different collections to avoid the "whole page" of being reloaded, I thought I might ask and maybe there is something I'm missing.

I'm on 0.9.0-rc10.

Community
  • 1
  • 1
Crenshinibon
  • 187
  • 1
  • 5

1 Answers1

0

You should not put the subscribe calls in Meteor.autorun.

Meteor.autorun is a reactive context, meaning everything inside will get re-run if a reactive data source changes inside. We’re storing the channel we’re in inside the Session under "current_channel". If that session value changes, then the subscription is renewed and we have access to different messages! src

Instead do something like this in your javascript:

Template.viewContent.movies = function () {
    var subscription = Meteor.subscribe('movies');
    return {
        data: Movies.find(),
        ready: subscription.ready
    };
}

Then your template will look like this:

 {{#each movies}}
      {{#if ready}}
           {{#each data}}
               #DOSOMETHING WITH THE MOVIES#
           {{/each}}
      {{else}}
           !!!Loading indicator here!!!
      {{/if}}
 {{/each}}
Marco de Jongh
  • 5,270
  • 3
  • 17
  • 29
  • Thanks so far. I thought about `Deps.autorun` being the source as well. But with your proposed solution I'm loosing the functionality of automatically updating a users view, when another user adds a movie, right? – Crenshinibon Aug 21 '14 at 13:43
  • @Crenshinibon No my solution doesn't break reactivity. the subscription still gets automatically updated when new data is added on the server. Try inserting extra stuff via the mongo console – Marco de Jongh Aug 21 '14 at 13:46
  • I wonder what `Meteor.autorun` is for then. I will test it and get in touch later. – Crenshinibon Aug 21 '14 at 14:01
  • @Crenshinibon Meteor.autorun references to Deps.autorun http://docs.meteor.com/#deps_autorun . It runs the given function now and reruns it whenever a dependency changes – Marco de Jongh Aug 21 '14 at 14:03