Chet
Chet

Reputation: 19829

Can I determine only what data has changed in a subscription in Meteor?

So I have a meteor collection full of GPS coordinates which I plot on a map. Anytime data changes in my Meteor collection, I have a deps.autorun that clears all the markers that aren't in my subscription. Then I draw all the markers that have yet to be drawn. If I have a bunch of data that updates quickly, the browser on my iPhone crashes. So I would like to be able to be able to update the map for only what has changed, rather than scanning each time and finding what has changed to lighten the load. Is this possible?

Thanks

Upvotes: 0

Views: 144

Answers (1)

imslavko
imslavko

Reputation: 6676

@user728291 is right, observeChanges or just observe is used in such situations.

What you want to do is to track all new objects by ids:

Template.map.rendered = function () {
  Places.find().observe({
    added: function (doc) {
      // code to add pin of doc
    },
    changed: function (newDoc, oldDoc) {
      // code to move pin from oldDoc position to newDoc
    },
    removed: function (doc) {
      // code to remove a pin associated with doc
    }
  });
}

In fact, whenever you combine {{#each things}}...{{/each}} template helper with your data returned as MongoDB cursor:

Template.myTemplate.things = function () {
  return Things.find({ rating: { $gt: 3 } });
}

Meteor will do the same thing under the hood. UI system will observe the changes on the cursor an instead of redrawing everything, it will figure out what changed and update only relevant parts of your displayed list.

Upvotes: 2

Related Questions