/meteor-subscription-data

Reactive and shared subscription data context

Primary LanguageCoffeeScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

subscription data context

This Meteor smart package provides a reactive and shared (between client and server side) subscription data context. When you subscribe to a publish endpoint, a reactive data context is established which can be read and changed both on the client or server side to provide alternative (reactive) way of passing arguments to the publish endpoint, without subscription restarting. To be useful, use peerlibrary:reactive-publish and server-side autorun in your publish endpoint function to react to data context changes.

Adding this package to your Meteor application extends publish endpoint function's this and subscription's handle with data and setData methods.

Both client and server side.

Installation

meteor add peerlibrary:subscription-data

API

The subscription handle returned from Meteor.subscribe contains two new methods:

  • data(path) – returns current data context object; if path is specified, returns value under path in the data context; it uses data-lookup package to resolve the path
  • setData(path, value) - sets the value under path in the data context object to value; if value is undefined, path is unset; alternatively, you can pass the whole new data context object which will be used as the new data context

Same methods are available also inside the publish endpoint function through this.

Examples

If on the server side you have such publish endpoint:

Meteor.publish('infinite-scroll', function () {
  var self = this;

  self.autorun(function (computation) {
    self.setData('countAll', MyCollection.find().count());
  });

  self.autorun(function (computation) {
    var limit = self.data('limit') || 10;
    // Do not forget to check the data.
    check(limit, Number);

    return MyCollection.find({}, {limit: limit, sort: {timestamp: -1}});
  });
});

Then you can on the client side subscribe to it and control it without restarting the subscription:

var subscription = Meteor.subscribe('infinite-scroll');

// Returns the count of all documents in the
// collection, even if only a subset is published.
subscription.data('countAll');

// Sets a new limit for published documents. Only the extra documents
// are send to the client and subscription does not restart.
subscription.setData('limit', 20);