Skip to content

Commit

Permalink
Merge pull request #1346 from creynders/grappling-hook
Browse files Browse the repository at this point in the history
Swap `prepost` for `grappling-hook`
  • Loading branch information
JedWatson committed Apr 27, 2015
2 parents 5a9db39 + aa76c9b commit 101d7ca
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 137 deletions.
10 changes: 4 additions & 6 deletions fields/types/azurefile/AzureFileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _ = require('underscore'),
util = require('util'),
azure = require('azure'),
utils = require('keystone-utils'),
prepost = require('../../../lib/prepost'),
grappling = require('grappling-hook'),
super_ = require('../Type');


Expand All @@ -19,8 +19,8 @@ var _ = require('underscore'),
*/

function azurefile(list, path, options) {
prepost.mixin(this)
.register('pre:upload');
grappling.mixin(this)
.allowHooks('pre:upload');

this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';
Expand Down Expand Up @@ -268,9 +268,7 @@ azurefile.prototype.uploadFile = function(item, file, update, callback) {
});
};

this.hooks('pre:upload', function(fn, next) {
fn(item, file, next);
}, function(err) {
this.callHook('pre:upload', [item, file], function(err) {
if (err) return callback(err);
doUpload();
});
Expand Down
18 changes: 5 additions & 13 deletions fields/types/localfile/LocalFileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var fs = require('fs-extra'),
path = require('path'),
_ = require('underscore'),
moment = require('moment'),
prepost = require('../../../lib/prepost'),
grappling = require('grappling-hook'),
util = require('util'),
utils = require('keystone-utils'),
super_ = require('../Type');
Expand All @@ -18,8 +18,8 @@ var fs = require('fs-extra'),
*/

function localfile(list, path, options) {
prepost.mixin(this)
.register('pre:move', 'post:move');
grappling.mixin(this)
.allowHooks('move');
this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';

Expand Down Expand Up @@ -291,23 +291,15 @@ localfile.prototype.uploadFile = function(item, file, update, callback) {
});
};

field.hooks('pre:move', function(fn, next) {
fn(item, file, next);
}, function(err) {

field.callHook('pre:move', [item, file], function(err) {
if (err) return callback(err);

doMove(function(err, fileData) {
if (err) return callback(err);

field.hooks('post:move', function(fn, next) {
fn(item, file, fileData, next);
}, function(err) {
field.callHook('post:move', [item, file, fileData], function(err) {
if (err) return callback(err);
callback(null, fileData);
});
});

});
};

Expand Down
15 changes: 5 additions & 10 deletions fields/types/localfiles/LocalFilesType.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var fs = require('fs-extra'),
utils = require('keystone-utils'),
super_ = require('../Type'),
async = require('async'),
prepost = require('../../../lib/prepost');
grappling = require('grappling-hook');

/**
* localfiles FieldType Constructor
Expand All @@ -20,8 +20,8 @@ var fs = require('fs-extra'),
*/

function localfiles(list, path, options) {
prepost.mixin(this)
.register('pre:move', 'post:move');
grappling.mixin(this)
.allowHooks('move');
this._underscoreMethods = ['format', 'uploadFiles'];
this._fixedSize = 'full';

Expand Down Expand Up @@ -315,17 +315,12 @@ localfiles.prototype.uploadFiles = function(item, files, update, callback) {

};

field.hooks('pre:move', function(fn, next) {
fn(item, file, next);
}, function(err) {
field.callHook('pre:move', [item, file], function(err) {
if (err) return processedFile(err);

doMove(function(err, fileData) {
if (err) return processedFile(err);

field.hooks('post:move', function(fn, next) {
fn(item, file, fileData, next);
}, function(err) {
field.callHook('post:move', [item, file, fileData], function(err) {
return processedFile(err, fileData);
});
});
Expand Down
10 changes: 4 additions & 6 deletions fields/types/s3file/S3FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ = require('underscore'),
knox = require('knox'),
// s3 = require('s3'),
utils = require('keystone-utils'),
prepost = require('../../../lib/prepost'),
grappling = require('grappling-hook'),
super_ = require('../Type');

/**
Expand All @@ -19,8 +19,8 @@ var _ = require('underscore'),
*/

function s3file(list, path, options) {
prepost.mixin(this)
.register('pre:upload');
grappling.mixin(this)
.allowHooks('pre:upload');
this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';

Expand Down Expand Up @@ -424,9 +424,7 @@ s3file.prototype.uploadFile = function(item, file, update, callback) {
});
};

this.hooks('pre:upload', function(fn, next) {
fn(item, file, next);
}, function(err) {
this.callHook('pre:upload', [item, file, next], function(err) {
if (err) return callback(err);
doUpload();
});
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var fs = require('fs'),
_ = require('underscore'),
express = require('express'),
utils = require('keystone-utils'),
prepost = require('./lib/prepost');
grappling = require('grappling-hook');

/**
* Don't use process.cwd() as it breaks module encapsulation
Expand All @@ -24,8 +24,8 @@ var moduleRoot = (function(_rootPath) {
* @api public
*/
var Keystone = function() {
prepost.mixin(this)
.register('pre:routes', 'pre:render');
grappling.mixin(this)
.allowHooks('pre:routes', 'pre:render');
this.lists = {};
this.paths = {};
this._options = {
Expand Down
17 changes: 4 additions & 13 deletions lib/core/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var _ = require('underscore'),
multer = require('multer'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
prepost = require('../prepost'),
compression = require('compression');

var dashes = '\n------------------------------------------------\n';
Expand Down Expand Up @@ -411,18 +410,10 @@ function mount(mountPath, parentApp, events) {
}

// Pre-route middleware
this.hooks('pre:routes', function(fn, next) {
try {
debug('adding pre-route middlewares');
app.use(fn);
}
catch(e) {
if (keystone.get('logger')) {
console.log('Invalid pre-route middleware provided');
}
throw e;
}
next();

app.use(function(req, res, next){
debug('adding pre-route middlewares');
keystone.callHook('pre:routes', [req,res], next);
});

// unless the headless option is set (which disables the Admin UI),
Expand Down
84 changes: 0 additions & 84 deletions lib/prepost.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,10 @@ View.prototype.render = function(renderFn, locals, callback) {
var preRenderQueue = [];

// Add Keystone's global pre('render') queue
keystone.hooks('pre:render', function(fn, next) {
keystone.getMiddleware('pre:render').forEach( function(fn) {
preRenderQueue.push(function(next) {
fn(req, res, next);
});
next();
});

this.initQueue.push(preRenderQueue);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"express": "~4.12.3",
"express-session": "~1.11.1",
"fs-extra": "~0.18.2",
"grappling-hook": "^2.1.0",
"gulp-streamify": "0.0.5",
"gulp-uglify": "^1.2.0",
"jade": "~1.9.2",
Expand Down

0 comments on commit 101d7ca

Please sign in to comment.