Magnus
Magnus

Reputation: 1184

Setting {reactive: false} when searching a collection in Meteor still updates the template

I've got a news feed idea I'm trying out in Meteor, but I'm having issues with making the damn thing behave :) I want it to load up the news feed on page load / refresh, but not when the data changes. I found in the documentation that adding {reactive: false} to the find method of a collection should make it stick to the results generated on render, but it doesn't seem to work for me. Meteor keeps updating the template right away.

Here's the code I've got:

On the server side:

Meteor.publish("newsfeed", function () {
    return Newsfeed.find({});
});

On the client side:

Meteor.subscribe('newsfeed');

Template.feed.feed_data = function() {
    var feed = Newsfeed.find({}, {
        sort: {updated_time: -1},
        limit: 10,
        reactive: false
    });

    return feed;
};

In the template:

<template name="feed">
    <div id="feed-wrapper">
        <ul>
            {{#each feed_data}}
                <li>
                    <div class="message">{{message}}</div>
                </li>
            {{/each}}
        </ul>
    </div>
</template>

If I then run Newsfeed.update({_id: 'some_random_id'}, {$set: {date_created: 'some_random_date'}}) in Dev Tools, the template updates the sorting of my news feed and changes it.

How do I make it not do that? :D

Upvotes: 1

Views: 3025

Answers (2)

David Glasser
David Glasser

Reputation: 1468

This is arguably a bug in Meteor. Passing reactive: false means that minimongo itself doesn't set up some code to say "observe and if it changes, invalidate". But #each has its own separate observeChanges call which uses the observe callbacks directly (not a reactive "invalidate and recalculate") to update the list. Probably we should not do this if the cursor has reactive: false on it. Track this in https://github.com/meteor/meteor/issues/771 !

Upvotes: 3

Tarang
Tarang

Reputation: 75945

Thats a bit odd, it ought to work. You could also use preserve:

Try adding this line in your client js

Template.feed.preserve(['#feed-wrapper']);

Btw is the template name="feed" in another template? Does this template have any reactive variables in it?

Upvotes: 0

Related Questions