example.js 653 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Example.
  3. */
  4. var before = require('./');
  5. // an object with some method
  6. var speak = {
  7. greeting: 'welcome',
  8. greet: function(a, b, fn){
  9. fn(null, a + ' ' + b);
  10. }
  11. };
  12. // factory before hook method
  13. speak.before = before(speak);
  14. // before greet
  15. speak.before('greet', function(args, fn){
  16. args[0] = args[0] + ' to this wonderful';
  17. fn();
  18. });
  19. // one more, this will run first
  20. speak.before('greet', function(args, fn){
  21. args[0] = args[0] + ', ' + this.greeting;
  22. fn();
  23. });
  24. // runs mutation hooks, then greets
  25. speak.greet('Human', 'world!', function(err, result){
  26. console.log(result); // => Human, welcome to this wonderful world!
  27. });