Remove model events

This commit is contained in:
Candy 2016-06-06 13:33:01 -04:00
parent ea2266e453
commit 3daca1e960
5 changed files with 14 additions and 118 deletions

View File

@ -61,4 +61,17 @@ silently ignored. This has been fixed in 3.0, therefore an undefined mixin
applied to a LoopBack model will throw an error that needs to be handled.
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/944)
for more details.
##Remove deprecated model hooks and model events
The following deprecated model events are now removed. Please upgrade your code by using
operation hooks in its place.
Model events
* changed
* deletedAll
* deleted
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/965)
for more details.

View File

@ -374,7 +374,6 @@ DataAccessObject.create = function(data, options, cb) {
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if (!err) Model.emit('changed', obj);
});
});
});
@ -583,9 +582,6 @@ DataAccessObject.upsert = function(data, options, cb) {
}
if (err) {
cb(err, obj);
if (!err) {
Model.emit('changed', inst);
}
} else {
var context = {
Model: Model,
@ -597,9 +593,6 @@ DataAccessObject.upsert = function(data, options, cb) {
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if (!err) {
Model.emit('changed', inst);
}
});
}
});
@ -778,8 +771,6 @@ DataAccessObject.replaceOrCreate = function replaceOrCreate(data, options, cb) {
};
Model.notifyObserversOf('after save', context, function(err) {
if (!err) Model.emit('changed', inst);
cb(err, obj, info);
});
}
@ -904,7 +895,6 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
} else {
cb(err, obj, created);
}
if (!err) Model.emit('changed', obj);
});
} else {
if (cb.promise) {
@ -1972,8 +1962,6 @@ DataAccessObject.destroyAll = function destroyAll(where, options, cb) {
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err)
Model.emit('deletedAll', whereIsEmpty(where) ? undefined : where);
});
}
}
@ -2040,7 +2028,6 @@ DataAccessObject.deleteById = function deleteById(id, options, cb) {
}
cb(null, info);
Model.emit('deleted', id);
});
return cb.promise;
};
@ -2248,9 +2235,6 @@ DataAccessObject.prototype.save = function(options, cb) {
updateDone.call(inst, function() {
saveDone.call(inst, function() {
cb(err, inst);
if (!err) {
Model.emit('changed', inst);
}
});
});
});
@ -2514,7 +2498,6 @@ DataAccessObject.prototype.remove =
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err) Model.emit('deleted', id);
});
});
return;
@ -2541,7 +2524,6 @@ DataAccessObject.prototype.remove =
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err) Model.emit('deleted', id);
});
});
}
@ -2751,8 +2733,6 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
options: options,
};
Model.notifyObserversOf('after save', context, function(err) {
if (!err) Model.emit('changed', inst);
cb(err, inst);
});
});
@ -2941,7 +2921,6 @@ function(data, options, cb) {
options: options,
};
Model.notifyObserversOf('after save', context, function(err) {
if (!err) Model.emit('changed', inst);
cb(err, inst);
});
});

View File

@ -189,20 +189,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
events.setMaxListeners(32);
for (var f in EventEmitter.prototype) {
if (typeof EventEmitter.prototype[f] === 'function') {
if (f !== 'on') {
ModelClass[f] = EventEmitter.prototype[f].bind(events);
continue;
}
// report deprecation warnings at the time Model.on() is called
ModelClass.on = function(event) {
if (['changed', 'deleted', 'deletedAll'].indexOf(event) !== -1) {
deprecated(this.modelName + '\'s event "' + event + '" ' +
'is deprecated, use Operation hooks instead. ' +
'http://docs.strongloop.com/display/LB/Operation+hooks');
}
EventEmitter.prototype.on.apply(events, arguments);
};
ModelClass[f] = EventEmitter.prototype[f].bind(events);
}
}
hiddenProperty(ModelClass, 'modelName', className);

View File

@ -2726,7 +2726,6 @@ EmbedsMany.prototype.destroyById = function(fkId, options, cb) {
if (err) return cb(err);
modelTo.notifyObserversOf('after delete', context, function(err) {
cb(err);
modelTo.emit('deleted', inst.id, inst.toJSON());
});
});
});

View File

@ -1,82 +0,0 @@
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var should = require('./init.js');
describe('events', function() {
beforeEach(function(done) {
var test = this;
this.db = getSchema();
this.TestModel = this.db.define('TestModel');
this.db.automigrate(function(err) {
if (err) return done(err);
test.TestModel.create(function(err, inst) {
if (err) return done(err);
test.inst = inst;
done();
});
});
this.shouldEmitEvent = function(eventName, listener, done) {
this.TestModel.on(eventName, function() {
listener.apply(this, arguments);
done();
});
};
});
describe('changed', function() {
it('should be emitted after save', function(done) {
var model = new this.TestModel({ name: 'foobar' });
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
model.save();
});
it('should be emitted after upsert', function(done) {
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
this.TestModel.upsert({ name: 'batbaz' });
});
it('should be emitted after create', function(done) {
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
this.TestModel.create({ name: '...' });
});
it('should be emitted after updateAttributes', function(done) {
var test = this;
this.TestModel.create({ name: 'bazzy' }, function(err, model) {
// prevent getting the changed event from "create"
process.nextTick(function() {
test.shouldEmitEvent('changed', assertValidChangedArgs, done);
model.updateAttributes({ name: 'foo' });
});
});
});
});
describe('deleted', function() {
it('should be emitted after destroy', function(done) {
this.shouldEmitEvent('deleted', assertValidDeletedArgs, done);
this.inst.destroy();
});
it('should be emitted after deleteById', function(done) {
this.shouldEmitEvent('deleted', assertValidDeletedArgs, done);
this.TestModel.deleteById(this.inst.id);
});
});
describe('deletedAll', function() {
it('should be emitted after destroyAll', function(done) {
this.shouldEmitEvent('deletedAll', function(where) {
where.name.should.equal('foo');
}, done);
this.TestModel.destroyAll({ name: 'foo' });
});
});
});
function assertValidChangedArgs(obj) {
obj.should.have.property('id');
}
function assertValidDeletedArgs(id) {
id.should.be.ok;
}