mirror of
https://github.com/feathersjs/feathers.git
synced 2026-02-01 17:37:38 +00:00
forgot that services become event emitters via the events mixin
This commit is contained in:
parent
1a41ae151c
commit
ab2ac7878f
@ -1,5 +1,4 @@
|
||||
var Proto = require('uberproto');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var util = require('util');
|
||||
var error = require('../errors');
|
||||
var _ = require('underscore');
|
||||
@ -9,7 +8,7 @@ var _ = require('underscore');
|
||||
// you be able to sort by multiple attributes?
|
||||
//
|
||||
// ie. { sort: ['name', 'birthday'], order: 'ascending'}
|
||||
var mutators = {
|
||||
var filters = {
|
||||
sort: function (values, param) {
|
||||
return _.sortBy(values, function (item) {
|
||||
return item[param];
|
||||
@ -42,7 +41,7 @@ var MemoryService = Proto.extend({
|
||||
|
||||
var values = _.values(this.store);
|
||||
|
||||
_.each(mutators, function(handler, name) {
|
||||
_.each(filters, function(handler, name) {
|
||||
values = params[name] ? handler(values, params[name]) : values;
|
||||
});
|
||||
|
||||
@ -70,14 +69,12 @@ var MemoryService = Proto.extend({
|
||||
data[this._id] = id;
|
||||
|
||||
if (this.store[id]){
|
||||
if (cb) cb(new Error('A record with id: ' + id + ' already exists'));
|
||||
return;
|
||||
return cb(new Error('A record with id: ' + id + ' already exists'));
|
||||
}
|
||||
|
||||
this.store[id] = data;
|
||||
|
||||
this.emit('created', data);
|
||||
if (cb) cb(null, data);
|
||||
cb(null, data);
|
||||
},
|
||||
|
||||
update: function (id, data, cb) {
|
||||
@ -87,14 +84,10 @@ var MemoryService = Proto.extend({
|
||||
self.store[id][key] = value;
|
||||
});
|
||||
|
||||
this.emit('updated', this.store[id]);
|
||||
|
||||
if (cb) cb(null, this.store[id]);
|
||||
return;
|
||||
return cb(null, this.store[id]);
|
||||
}
|
||||
|
||||
this.emit('error', 'Could not find record with ' + id);
|
||||
if (cb) cb('Could not find record with ' + id);
|
||||
cb('Could not find record with ' + id);
|
||||
},
|
||||
|
||||
destroy: function (id, params, cb) {
|
||||
@ -106,15 +99,11 @@ var MemoryService = Proto.extend({
|
||||
var deleted = this.store[id];
|
||||
delete this.store[id];
|
||||
|
||||
this.emit('destroyed', deleted);
|
||||
|
||||
if (cb) cb(null, deleted);
|
||||
return;
|
||||
return cb(null, deleted);
|
||||
}
|
||||
|
||||
this.emit('error', 'Could not find record with ' + id);
|
||||
if (cb) cb('Could not find record with ' + id);
|
||||
cb('Could not find record with ' + id);
|
||||
}
|
||||
}, EventEmitter.prototype);
|
||||
});
|
||||
|
||||
module.exports = MemoryService;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user