mondora / asteroid

An alternative client for a Meteor backend
MIT License
734 stars 101 forks source link

Events other than "changed" #74

Open Type1J opened 9 years ago

Type1J commented 9 years ago

The Meteor framework has 3 events on its observers: "added", "changed", and "removed" that match the DDP messages. However, unlike the DDP messages, the events pass the whole object that the event affects.

Asteroid has all of this information, I believe, but the ReactiveQuery only emits 1 event: "change". I'd like to be able to reuse some code that used the native Meteor DDP client, and I'm finding myself reconstructing information that I know Asteroid has. For example, using underscore:

query.on("change", function(id) {
  var data = _(this.result).last();
  if (data._id != id) {
    data = _.find(this.result, {_id: id});
  }

  // Dispatch to other 
  if (_(data).isUndefined()) {
    onDataWasRemovedWithId(id);
  } else {
    onDataWasAddedOrChanged(data);
  }
});

I wouldn't have created an issue if I could have reconstructed the "on...()" method calls exactly as the "added", "changed", and "removed" event handlers, but these 2 functions are the closest that I could create to those 3 signatures.

Can "added", "changed", and "removed" like events be added to ReactiveQuery easily?