Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate model hooks and events #473

Merged
merged 2 commits into from
Feb 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OPTS = --growl
TESTS = test/*.test.js

test:
$(TESTER) $(OPTS) $(TESTS)
NO_DEPRECATION=loopback-datasource-juggler $(TESTER) $(OPTS) $(TESTS)
test-verbose:
$(TESTER) $(OPTS) --reporter spec $(TESTS)
testing:
Expand Down
16 changes: 16 additions & 0 deletions lib/browser.depd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// A lightweight alternative to "depd" that works in the browser
module.exports = function depd(namespace) {
var warned = {};
return function deprecate(message) {
if (warned[message]) return;
warned[message] = true;

if (process.noDeprecation) {
return;
} else if (process.traceDeprecation) {
console.trace(namespace, 'deprecated', message);
} else {
console.warn(namespace, 'deprecated', message);
}
};
};
22 changes: 22 additions & 0 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var deprecated = require('depd')('loopback-datasource-juggler');

/*!
* Module exports
*/
Expand Down Expand Up @@ -39,6 +41,13 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback)
}
var inst = this;

if (actionName !== 'initialize') {
if (beforeHook)
deprecateHook(inst.constructor, ['before', 'pre'], capitalizedName);
if (afterHook)
deprecateHook(inst.constructor, ['after', 'post'], capitalizedName);
}

// we only call "before" hook when we have actual action (work) to perform
if (work) {
if (beforeHook) {
Expand Down Expand Up @@ -71,3 +80,16 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback)
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function deprecateHook(ctor, prefixes, capitalizedName) {
var candidateNames = prefixes.map(function(p) { return p + capitalizedName; });
if (capitalizedName === 'Validate')
candidateNames.push(prefixes[0] + 'Validation');

var hookName = candidateNames.filter(function(hook) { return !!ctor[hook]; })[0];
if (!hookName) return; // just to be sure, this should never happen
if (ctor.modelName) hookName = ctor.modelName + '.' + hookName;
deprecated('Model hook "' + hookName + '" is deprecated, ' +
'use Operation hooks instead. ' +
'http://docs.strongloop.com/display/LB/Operation+hooks');
}
16 changes: 15 additions & 1 deletion lib/model-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"url": "https://github.com/strongloop/loopback-datasource-juggler"
},
"main": "index.js",
"browser": {
"depd": "./lib/browser.depd.js"
},
"scripts": {
"test": "make test"
},
Expand All @@ -30,6 +33,7 @@
"dependencies": {
"async": "^0.9.0",
"debug": "^2.1.1",
"depd": "^1.0.0",
"inflection": "^1.6.0",
"lodash": "~3.0.1",
"loopback-connector": "1.x",
Expand Down