Skip to content

Commit

Permalink
Remove model events
Browse files Browse the repository at this point in the history
  • Loading branch information
0candy committed Jun 6, 2016
1 parent b039b51 commit f9364df
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 79 deletions.
12 changes: 12 additions & 0 deletions 3.0-RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 0 additions & 18 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down Expand Up @@ -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);
}
});
}
});
Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
}
Expand Down Expand Up @@ -2040,7 +2031,6 @@ DataAccessObject.deleteById = function deleteById(id, options, cb) {
}

cb(null, info);
Model.emit('deleted', id);
});
return cb.promise;
};
Expand Down Expand Up @@ -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);
}
});
});
});
Expand Down Expand Up @@ -2514,7 +2501,6 @@ DataAccessObject.prototype.remove =
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err) Model.emit('deleted', id);
});
});
return;
Expand All @@ -2541,7 +2527,6 @@ DataAccessObject.prototype.remove =
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err) Model.emit('deleted', id);
});
});
}
Expand Down Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});
Expand Down
15 changes: 1 addition & 14 deletions lib/model-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion lib/relation-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});
});
Expand Down
46 changes: 0 additions & 46 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f9364df

Please sign in to comment.