Untitled


SUBMITTED BY: Guest

DATE: Dec. 10, 2013, 12:01 a.m.

FORMAT: Text only

SIZE: 1.5 kB

HITS: 2125

  1. var manager = function(db) {
  2. this.db = db;
  3. }
  4. manager.prototype = {
  5. doSth: function(cb) {
  6. this.db.findOne({}, function(err, doc) {
  7. // more logic here
  8. cb(doc);
  9. };
  10. }
  11. }
  12. module.exports = manager;
  13. manager = require('manager');
  14. var db = require('mongo');
  15. db.connect(); //forget the details here
  16. var M = new manager(db);
  17. M.doSth();
  18. manager = require('manager');
  19. var M = new manager(app.get('db')); //ask the app for a resource or connect db again
  20. M.doSth();
  21. var manager = function() {
  22. this.db = null;
  23. }
  24. manager.prototype = {
  25. connect: function(db) {
  26. if (this.db == null)
  27. this.db = db;
  28. }
  29. doSth: function(cb) {
  30. this.db.findOne({}, function(err, doc) {
  31. // more logic here
  32. cb(doc);
  33. };
  34. }
  35. }
  36. module.exports = manager;
  37. M = require('manager'); //is already an instance
  38. var db = require('mongo').connect();
  39. M.connect(db)
  40. M.doSth();
  41. M = require('manager');
  42. M.doSth(); //will work since manager has been connected by app.js

comments powered by Disqus