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,17 +625,15 @@ module.exports = function(User) {
} }
); );
UserModel.on('attached', function() { UserModel.afterRemote('confirm', function(ctx, inst, next) {
UserModel.afterRemote('confirm', function(ctx, inst, next) { if (ctx.args.redirect !== undefined) {
if (ctx.args.redirect !== undefined) { if (!ctx.res) {
if (!ctx.res) { return next(new Error('The transport does not support HTTP redirects.'));
return next(new Error('The transport does not support HTTP redirects.'));
}
ctx.res.location(ctx.args.redirect);
ctx.res.status(302);
} }
next(); ctx.res.location(ctx.args.redirect);
}); ctx.res.status(302);
}
next();
}); });
// default models // default models

View File

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