From f9364df0c4d3c6225fd83d0f1173dd735e8320cd Mon Sep 17 00:00:00 2001 From: Candy Date: Mon, 6 Jun 2016 13:33:01 -0400 Subject: [PATCH] Remove model events --- 3.0-RELEASE-NOTES.md | 12 ++++++++++ lib/dao.js | 18 --------------- lib/model-builder.js | 15 +------------ lib/relation-definition.js | 1 - test/events.js | 46 -------------------------------------- 5 files changed, 13 insertions(+), 79 deletions(-) diff --git a/3.0-RELEASE-NOTES.md b/3.0-RELEASE-NOTES.md index c518ed34d..0d8e24582 100644 --- a/3.0-RELEASE-NOTES.md +++ b/3.0-RELEASE-NOTES.md @@ -61,4 +61,16 @@ 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. + +Model events +*changed +*deletedAll +*deleted + +See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/) for more details. \ No newline at end of file diff --git a/lib/dao.js b/lib/dao.js index 0550b8533..5a133b8a8 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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); }); }); }); @@ -597,9 +596,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 +774,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 +898,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 +1965,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 +2031,6 @@ DataAccessObject.deleteById = function deleteById(id, options, cb) { } cb(null, info); - Model.emit('deleted', id); }); return cb.promise; }; @@ -2248,9 +2238,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 +2501,6 @@ DataAccessObject.prototype.remove = }; Model.notifyObserversOf('after delete', context, function(err) { cb(err, info); - if (!err) Model.emit('deleted', id); }); }); return; @@ -2541,7 +2527,6 @@ DataAccessObject.prototype.remove = }; Model.notifyObserversOf('after delete', context, function(err) { cb(err, info); - if (!err) Model.emit('deleted', id); }); }); } @@ -2751,8 +2736,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 +2924,6 @@ function(data, options, cb) { options: options, }; Model.notifyObserversOf('after save', context, function(err) { - if (!err) Model.emit('changed', inst); cb(err, inst); }); }); diff --git a/lib/model-builder.js b/lib/model-builder.js index c969c7cf0..480a25425 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -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); diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 69a2d1afa..fd331320a 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -2677,7 +2677,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()); }); }); }); diff --git a/test/events.js b/test/events.js index 21d2cb5a6..117ef9228 100644 --- a/test/events.js +++ b/test/events.js @@ -25,52 +25,6 @@ describe('events', function() { }); }; }); - - 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) {