Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Question: Is it possible to pass parameter from update to collectionHook? #192

Closed thebarty closed 7 years ago

thebarty commented 8 years ago

Hi guys,

Is it possible to pass parameter from update to collectionHook?

This would be really handy for my useCase. Something like this:

// pass a parameter to on update that I can then read in hook
Collection.update(id, {$set: { something: 'new } }, { parameterForCollectionHook: 'coming from mars' } )

// read this parameter in hook, p.e.
Collection.after.update( (parameterForCollectionHook) => {
  console.log(parameterForCollectionHook)
})

Is there any chance to do this?

nicooprat commented 8 years ago

Absolutely:

Posts.update(_id, {
  $set: {
    title: 'test'
  }
}, {myOption: false});

And then:

this.collection.before.update((userId, doc, fieldNames, modifier, opts) => {
  if( opts.myOption ) {
    ...
  }
});

I think it's not possible with insert or remove though (for now?).

zimme commented 7 years ago

@nicooprat's answer is correct, and the reason insert and remove doesn't accept options is because the original api doesn't have options as arguments.