Skip to content

ziarno/provide-mixin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ProvideMixin

A mixin for mdg:validated-method to add additional arguments to the run function.

Install

meteor add ziarno:provide-mixin

Usage

SomeCollection.methods.someMethod = new ValidateMethod({
    name,
    mixins: [ProvideMixin],
    provide: function (args) {
      //whatever we return here will be added
      //as the last argument of the run function
      return SomeOtherCollection.findOne(args.someOtherCollectionId);
    },
    validate,
    run: function (args, someOtherCollectionItem) {
      //...
    }
});

Important note: If you're using more than one mixin that wrap the run function (ex. ziarno:restrict-mixin), make sure to add ProvideMixin as the last mixin in the mixins array - that way it will be called first, and will be able to provide arguments to other methods (such as condition() and error() in ziarno:restrict-mixin)

Example:

SomeCollection.methods.someMethod = new ValidateMethod({
    name,
    // mixins: [ProvideMixin, RestrictMixin], - wrong!
    mixins: [RestrictMixin, ProvideMixin], //correct
    provide: function (args) {
      return SomeOtherCollection.findOne(args.someOtherCollectionId);
    },
    restrictions: [
      {
        condition: function (args, someOtherCollectionItem) {
          return someOtherCollectionItem.owner !== this.userId;
        },
        error: function (args, someOtherCollectionItem) {
          return new Meteor.Error(this.name + '.unauthorized', 'Only owners can do stuff');
        },
      }
    ],
    validate,
    run: function (args, someOtherCollectionItem) {
      //...
    }
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published