Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any possibility to provide default mixins? #53

Open
hamzapurra opened this issue May 23, 2016 · 3 comments
Open

Any possibility to provide default mixins? #53

hamzapurra opened this issue May 23, 2016 · 3 comments

Comments

@hamzapurra
Copy link

hamzapurra commented May 23, 2016

I wouldn't want to pass the callPromise mixin for every method that I write, for example....

@stubailo
Copy link
Contributor

Yeah it's easy to create your own wrapper class that passes whatever you like! If you want I can whip up a code snippet for how to do that.

@hamzapurra
Copy link
Author

Sure! that would be helpful....

@todda00
Copy link

todda00 commented Aug 9, 2016

I am attempting this, but am running into an issue when I extend the ValidatedMethod class, for example this works:

import {Meteor} from 'meteor/meteor';
import {SchemaDefs} from '/imports/api';
import {ValidatedMethod} from 'meteor/mdg:validated-method';
import {simpleSchemaMixin} from 'meteor/rlivingston:simple-schema-mixin';
import {PermissionsMixin} from 'meteor/didericis:permissions-mixin';
import {Answers} from '/lib/collections';

const {SCHEMA_ID} = SchemaDefs;

export const saveAnswer = new ValidatedMethod({
  name: 'core.answers.save',
  mixins: [PermissionsMixin,simpleSchemaMixin],
  allow: true,
  schema: [
    SCHEMA_ID,
    {answer: {type: String}}
  ],
  run({_id,answer}){
    return Answers.upsert({question:_id,user:this.userId},{
      $set:{
        question: _id,
        user: this.userId,
        value: answer
      }
    });
  }
});

But extending the Validated class like this:

import {ValidatedMethod} from 'meteor/mdg:validated-method';
import {simpleSchemaMixin} from 'meteor/rlivingston:simple-schema-mixin';
import {PermissionsMixin} from 'meteor/didericis:permissions-mixin';

export class SecureMethod extends ValidatedMethod {
  constructor(methodDefinition) {
    if (Array.isArray(methodDefinition.mixins)) {
      methodDefinition.mixins = methodDefinition.mixins.concat(PermissionsMixin,simpleSchemaMixin);
    } else {
      methodDefinition.mixins = [PermissionsMixin,simpleSchemaMixin];
    }
    super(methodDefinition);
  }
}

And defining it like this:

import {Meteor} from 'meteor/meteor';
import {SecureMethod, SchemaDefs} from '/imports/api';
import {Answers} from '/lib/collections';

const {SCHEMA_ID} = SchemaDefs;

export const saveAnswer = new SecureMethod({
  name: 'core.answers.save',
  allow: true,
  schema: [
    SCHEMA_ID,
    {answer: {type: String}}
  ],
  run({_id,answer}){
    return Answers.upsert({question:_id,user:this.userId},{
      $set:{
        question: _id,
        user: this.userId,
        value: answer
      }
    });
  }
});

Throws an error when called:

TypeError: Cannot read property 'apply' of undefined
    at SecureMethod.call

which is referenced to this line:

return this.connection.apply(this.name, [args], this.applyOptions, callback);

Seems the properties are not being inherited correctly, I even tried using an empty constructor and it still doesn't work when calling SecureMethod:

export class SecureMethod extends ValidatedMethod {
  constructor(methodDefinition) {
    super(methodDefinition);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants