1

I am transforming my Meteor collection to a class I've created:

People = new Mongo.Collection('people', {
  transform: function(doc) {
    return new Person(doc);
  }
});

Person has a bunch of methods, some of which control getting the data:

var john = People.findOne({ name: 'John' });
john.getFullName();

I also have some data that is not needed as often:

john.getAddress();

To avoid subscribing to data that's not needed, I have the people subscription only return summary data:

Meteor.publish('people', function() {
  return People.find({}, { fields: { firstName: 1, lastName: 1, email: 1 } });
});

While address data is subscribed to through the publication people_addresses.

My question regarding granular subscriptions in this format is: Where should the subscription be handled?

Option 1: Callbacks

Set up the getAddress method to subscribe to the data if we don't already have it:

People.prototype.getAddress = function(callback, fail) {
  var that = this;

  if (this._address) {
    callback(this._address);
  } else {
    Meteor.subscribe('people_addresses', this.getName(), function() {
      /*
       * Need to either update the current object to reflect the data
       * or make the class reactive somehow... any ideas?
       */ 

      callback(that._address);
    });
  }
};

Use the method with a callback to access the data:

john.getAddress(function(address) {
  /* Use Johns address data. */ 
});

Option 2: Data Preparation

Here we would be in a place where we know we're going to need address data.

Meteor.subscribe('people_addresses', 'john' function() {
  var john = People.findOne({ name: 'john' }), 
    address;

  address = john.getAddress();

  /* Use John's address */
});

Option #1 hides the complexity, but would need an elegant solution for updating the object with the newly received address data.

It also requires callbacks (or promises if we go that route) where Option #2 only requires wrapping the code in the subscribe.

What are your thoughts on handling granular data subscriptions? Are either of these options viable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.