8000 Improved Mixin organization, updated tests and examples. by daffl · Pull Request #15 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/custom_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var users = [
];

var service = {
index : function (params, cb) {
find: function (params, cb) {
cb(null, users);
},

Expand Down
2 changes: 1 addition & 1 deletion example/rest_memory/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var feathers = require('../../lib/feathers');
var Proto = require('uberproto');
var memoryService = feathers.memory();
var memoryService = feathers.service.memory();
var express = require('express');

feathers.createServer({ port: 3000 })
Expand Down
10 changes: 7 additions & 3 deletions lib/mixins/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var eventMappings = {
*
* @type {{setup: Function}}
*/
var Mixin = {
var EventMixin = {
setup: function() {
var emitter = this._rubberDuck = rubberduck.emitter(this);
var self = this;
Expand Down Expand Up @@ -47,12 +47,16 @@ var Mixin = {

// Add EventEmitter prototype methods (if they don't already exist)
_.each(EventEmitter.prototype, function(fn, name) {
Mixin[name] = function() {
EventMixin[name] = function() {
if(this._super) {
return this._super.apply(this, arguments);
}
return EventEmitter.prototype[name].apply(this, arguments);
}
});

module.exports = Mixin;
module.exports = function(service) {
service.mixin && service.mixin(EventMixin);
};

module.exports.Mixin = EventMixin;
9 changes: 4 additions & 5 deletions lib/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
exports.Event = require('./event');
exports.Validation = require('./validation');

// TODO exports.Association = require('./association');
// TODO exports.Authentication = require('./authentication');
module.exports = [
require('./event'),
require('./validation')
];
10 changes: 9 additions & 1 deletion lib/mixins/validation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var _ = require('underscore');
var ValidationError = require('../errors').ValidationError;

module.exports = {
var ValidationMixin = {
create: function(data, params, cb) {
var self = this;
this.validate(data, _.extend({ validates: 'create' }, params), function(errors) {
Expand All @@ -22,3 +22,11 @@ module.exports = {
});
}
};

module.exports = function(service) {
if(typeof service.validate === 'function' && service.mixin) {
service.mixin(ValidationMixin);
}
};

module.exports.Mixin = ValidationMixin;
13 changes: 2 additions & 11 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ var Server = Proto.extend({
methods: [ 'find', 'get', 'create', 'update', 'destroy' ],
// Mixins to add when registering a service.
// An array of functions that get passed the service object and can do something with it.
mixins: [
function(service) {
service.mixin && service.mixin(mixins.Event);
},
function(service) {
if(typeof service.validate === 'function' && service.mixin) {
service.mixin(mixins.Validation);
}
}
]
mixins: mixins
});

this.services = {};
Expand Down Expand Up @@ -73,7 +64,7 @@ var Server = Proto.extend({
return this.config.app.get(setting);
},

service: function (location, service, options) {
service: function (location, service) {
var protoService = Proto.extend(service);

// Add all the mixins
Expand Down
15 changes: 8 additions & 7 deletions test/mixins/event.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var assert = require('assert');
var _ = require('underscore');
var Proto = require('uberproto');
var EventMixin = require('../../lib/mixins/event');
var mixinEvent = require('../../lib/mixins/event');
var EventMixin = mixinEvent.Mixin;

describe('Event mixin', function () {
it('initializes', function () {
Expand All @@ -11,7 +12,7 @@ describe('Event mixin', function () {
}
});

FixtureService.mixin(EventMixin);
mixinEvent(FixtureService);

assert.equal(typeof FixtureService.setup, 'function');
assert.equal(typeof FixtureService.on, 'function');
Expand All @@ -27,7 +28,7 @@ describe('Event mixin', function () {
},

emit: function() {
return 'Original emit'
return 'Original emit';
}
}

Expand All @@ -47,7 +48,7 @@ describe('Event mixin', function () {
}
});

FixtureService.mixin(EventMixin);
mixinEvent(FixtureService);

var instance = Proto.create.call(FixtureService);
instance.setup();
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('Event mixin', function () {
}
});

FixtureService.mixin(EventMixin);
mixinEvent(FixtureService);

var instance = Proto.create.call(FixtureService);
instance.setup();
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('Event mixin', function () {
}
});

FixtureService.mixin(EventMixin);
mixinEvent(FixtureService);

var instance = Proto.create.call(FixtureService);
instance.setup();
Expand All @@ -128,7 +129,7 @@ describe('Event mixin', function () {
}
});

FixtureService.mixin(EventMixin);
mixinEvent(FixtureService);

var instance = Proto.create.call(FixtureService);
instance.setup();
Expand Down
8 changes: 4 additions & 4 deletions test/mixins/validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert');
var _ = require('underscore');
var Proto = require('uberproto');
var errors = require('../../lib/errors');
var ValidationMixin = require('../../lib/mixins/validation');
var mixinValidation = require('../../lib/mixins/validation');

describe('Validation mixin', function () {
it('initializes', function () {
Expand All @@ -17,7 +17,7 @@ describe('Validation mixin', function () {
}
});

ValidationService.mixin(ValidationMixin);
mixinValidation(ValidationService);

assert.equal(typeof ValidationService.create, 'function');
assert.equal(typeof ValidationService.update, 'function');
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Validation mixin', function () {
}
});

ValidationService.mixin(ValidationMixin);
mixinValidation(ValidationService);

var instance = Proto.create.call(ValidationService);
instance.create({ name: 'Tester' }, {}, function(error, data) {
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('Validation mixin', function () {
}
});

ValidationService.mixin(ValidationMixin);
mixinValidation(ValidationService);

var instance = Proto.create.call(ValidationService);
instance.update(14, { name: 'Tester' }, {}, function(error, data) {
Expand Down
0