diff --git a/lib/model-builder.js b/lib/model-builder.js index 7063915f..9c56c571 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -6,6 +6,7 @@ var inflection = require('inflection'); var EventEmitter = require('events').EventEmitter; var util = require('util'); var assert = require('assert'); +var deprecated = require('depd')('loopback-datasource-juggler'); var DefaultModelBaseClass = require('./model.js'); var List = require('./list.js'); var ModelDefinition = require('./model-definition.js'); @@ -182,7 +183,20 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett events.setMaxListeners(32); for (var f in EventEmitter.prototype) { if (typeof EventEmitter.prototype[f] === 'function') { - ModelClass[f] = EventEmitter.prototype[f].bind(events); + 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); + }; } } hiddenProperty(ModelClass, 'modelName', className);