Code cleanup, add Model._runWhenAttachedToApp

This commit is contained in:
Miroslav Bajtoš 2015-04-03 10:04:05 +02:00
parent 5a51c7f0fa
commit a71c8253e2
2 changed files with 28 additions and 39 deletions

View File

@ -625,7 +625,6 @@ module.exports = function(User) {
}
);
UserModel.on('attached', function() {
UserModel.afterRemote('confirm', function(ctx, inst, next) {
if (ctx.args.redirect !== undefined) {
if (!ctx.res) {
@ -636,7 +635,6 @@ module.exports = function(User) {
}
next();
});
});
// default models
assert(loopback.Email, 'Email model must be defined before User model');

View File

@ -180,36 +180,32 @@ module.exports = function(registry) {
// before remote hook
ModelCtor.beforeRemote = function(name, fn) {
var self = this;
if (this.app) {
var remotes = this.app.remotes();
var className = self.modelName;
var className = this.modelName;
this._runWhenAttachedToApp(function(app) {
var remotes = app.remotes();
remotes.before(className + '.' + name, function(ctx, next) {
fn(ctx, ctx.result, next);
});
} else {
var args = arguments;
this.once('attached', function() {
self.beforeRemote.apply(self, args);
});
}
};
// after remote hook
ModelCtor.afterRemote = function(name, fn) {
var self = this;
if (this.app) {
var remotes = this.app.remotes();
var className = self.modelName;
var className = this.modelName;
this._runWhenAttachedToApp(function(app) {
var remotes = app.remotes();
remotes.after(className + '.' + name, function(ctx, next) {
fn(ctx, ctx.result, next);
});
} else {
var args = arguments;
this.once('attached', function() {
self.afterRemote.apply(self, args);
});
}
};
ModelCtor._runWhenAttachedToApp = function(fn) {
if (this.app) return fn(this.app);
var self = this;
self.once('attached', function() {
fn(self.app);
});
};
// resolve relation functions
@ -375,15 +371,10 @@ module.exports = function(registry) {
*/
Model.getApp = function(callback) {
var Model = this;
if (this.app) {
callback(null, this.app);
} else {
Model.once('attached', function() {
this._runWhenAttachedToApp(function(app) {
assert(Model.app);
callback(null, Model.app);
});
}
};
/**