Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports = module.exports = function loadModels(Y, models) {
- var NS = Y.namespace('TDP');
- return function(req, res, next) {
- var stack = new Y.Parallel(),
- oauth = req.session.oauth;
- if ( ! Y.Lang.isObject( req.models ) ) {
- req.models = { };
- }
- if ( !oauth ) {
- Y.log('No oauth in the session. You must define this first.', 'error');
- res.redirect('/login');
- return;
- }
- Y.Array.each( models, function(name) {
- // Assume this model is already loaded.
- if ( req.models[name] ) {
- return;
- }
- var model = new NS[name]();
- model.load(
- { headers : { 'X-Access-Token' : oauth.access_token } },
- stack.add( function(err) {
- if ( err ) {
- Y.log('Failed loading model ' + name + ' with error:');
- Y.log(err);
- } else {
- Y.log('Setup a model successfully');
- req.models[name] = model;
- }
- })
- );
- });
- stack.done( function() { next(); } );
- };
- };
Add Comment
Please, Sign In to add comment