Skip to content

Instantly share code, notes, and snippets.

@jboulhous
Forked from tmeasday/gist:4042603
Created December 4, 2012 13:03
Show Gist options
  • Save jboulhous/4203663 to your computer and use it in GitHub Desktop.
Save jboulhous/4203663 to your computer and use it in GitHub Desktop.
Meteor "Join" - Javascript
Meteor.publish 'paths', (since) ->
pointHandles = {}
publishPath = (pathId) =>
pointHandles[pathId] = Points.find({pathId: pathId}).observe
added: (obj) =>
@set('points', obj._id, obj)
@flush()
# these two should never happen
changed: (obj) =>
@set('points', obj._id, obj)
@flush()
removed: (old_obj) =>
@unset('points', old_obj._id, _.keys(old_obj))
@flush()
pathHandle = Paths.find({createdAt: {$gt: since}}).observe
added: (obj) =>
@set('paths', obj._id, obj)
@flush()
publishPath(obj._id)
# in general this should be smarter, but shouldn't happen much
changed: (obj) =>
@set('paths', obj._id, obj)
@flush()
# this should never happen, but just in case
removed: (old_obj) =>
pointHandles[old_obj._id].stop()
@unset('paths', old_obj._id, _.keys(old_obj))
@flush()
@complete()
@flush()
@onStop =>
handle.stop() for handle in pointHandles
pathHandle.stop()
@jboulhous
Copy link
Author

in Javascript (form coffeescript.org)

    Meteor.publish('paths', function(since) {
      var pathHandle, pointHandles, publishPath,
        _this = this;
      pointHandles = {};
      publishPath = function(pathId) {
        return pointHandles[pathId] = Points.find({
          pathId: pathId
        }).observe({
          added: function(obj) {
            _this.set('points', obj._id, obj);
            return _this.flush();
          },
          changed: function(obj) {
            _this.set('points', obj._id, obj);
            return _this.flush();
          },
          removed: function(old_obj) {
            _this.unset('points', old_obj._id, _.keys(old_obj));
            return _this.flush();
          }
        });
      };
      pathHandle = Paths.find({
        createdAt: {
          $gt: since
        }
      }).observe({
        added: function(obj) {
          _this.set('paths', obj._id, obj);
          _this.flush();
          return publishPath(obj._id);
        },
        changed: function(obj) {
          _this.set('paths', obj._id, obj);
          return _this.flush();
        },
        removed: function(old_obj) {
          pointHandles[old_obj._id].stop();
          _this.unset('paths', old_obj._id, _.keys(old_obj));
          return _this.flush();
        }
      });
      this.complete();
      this.flush();
      return this.onStop(function() {
        var handle, _i, _len;
        for (_i = 0, _len = pointHandles.length; _i < _len; _i++) {
          handle = pointHandles[_i];
          handle.stop();
        }
        return pathHandle.stop();
      });
    });

@jboulhous
Copy link
Author

i did not yet test this Javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment