From 3daca1e960edb855acc4adc2c4a255d1b2ecfd35 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 | 13 ++++++ lib/dao.js | 21 ---------- lib/model-builder.js | 15 +------ lib/relation-definition.js | 1 - test/events.js | 82 -------------------------------------- 5 files changed, 14 insertions(+), 118 deletions(-) delete mode 100644 test/events.js diff --git a/3.0-RELEASE-NOTES.md b/3.0-RELEASE-NOTES.md index c518ed34d..3811f2926 100644 --- a/3.0-RELEASE-NOTES.md +++ b/3.0-RELEASE-NOTES.md @@ -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. \ No newline at end of file diff --git a/lib/dao.js b/lib/dao.js index 0550b8533..daaf2f147 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); }); }); }); @@ -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); }); }); 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 f2a236c1e..d11e5c6ce 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -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()); }); }); }); diff --git a/test/events.js b/test/events.js deleted file mode 100644 index 21d2cb5a6..000000000 --- a/test/events.js +++ /dev/null @@ -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; -}