Guest User

Untitled

a guest
Dec 11th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. exports = module.exports = function loadModels(Y, models) {
  2. var NS = Y.namespace('TDP');
  3. return function(req, res, next) {
  4. var stack = new Y.Parallel(),
  5. oauth = req.session.oauth;
  6.  
  7. if ( ! Y.Lang.isObject( req.models ) ) {
  8. req.models = { };
  9. }
  10.  
  11. if ( !oauth ) {
  12. Y.log('No oauth in the session. You must define this first.', 'error');
  13. res.redirect('/login');
  14. return;
  15. }
  16.  
  17. Y.Array.each( models, function(name) {
  18. // Assume this model is already loaded.
  19. if ( req.models[name] ) {
  20. return;
  21. }
  22.  
  23. var model = new NS[name]();
  24. model.load(
  25. { headers : { 'X-Access-Token' : oauth.access_token } },
  26. stack.add( function(err) {
  27. if ( err ) {
  28. Y.log('Failed loading model ' + name + ' with error:');
  29. Y.log(err);
  30. } else {
  31. Y.log('Setup a model successfully');
  32. req.models[name] = model;
  33. }
  34. })
  35. );
  36. });
  37.  
  38. stack.done( function() { next(); } );
  39. };
  40. };
Add Comment
Please, Sign In to add comment