context.js 945 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const ContextView = require('../../lib/context_view');
  3. const VIEW = Symbol('Context#view');
  4. module.exports = {
  5. /**
  6. * Render a file, then set to body, the parameter is same as {@link @ContextView#render}
  7. * @return {Promise} result
  8. */
  9. render(...args) {
  10. return this.renderView(...args).then(body => {
  11. this.body = body;
  12. });
  13. },
  14. /**
  15. * Render a file, same as {@link @ContextView#render}
  16. * @return {Promise} result
  17. */
  18. renderView(...args) {
  19. return this.view.render(...args);
  20. },
  21. /**
  22. * Render template string, same as {@link @ContextView#renderString}
  23. * @return {Promise} result
  24. */
  25. renderString(...args) {
  26. return this.view.renderString(...args);
  27. },
  28. /**
  29. * View instance that is created every request
  30. * @member {ContextView} Context#view
  31. */
  32. get view() {
  33. if (!this[VIEW]) {
  34. this[VIEW] = new ContextView(this);
  35. }
  36. return this[VIEW];
  37. },
  38. };