Source: mvc/Renderer.js

  1. // Copyright 2011 Tart. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('tart.mvc.Renderer');
  15. goog.require('tart.mvc.Action');
  16. goog.require('tart.mvc.Layout');
  17. goog.require('tart.mvc.View');
  18. goog.require('tart.mvc.uri.Redirection');
  19. /**
  20. * @constructor
  21. * @param {tart.mvc.LayoutTemplate} layout The default layout of the application. This layout will be used when an
  22. * action chooses not to set its own layout.
  23. * @param {Element} dom DOM element this renderer will render the application in.
  24. */
  25. tart.mvc.Renderer = function(layout, dom) {
  26. this.defaultLayout = layout;
  27. this.dom_ = dom;
  28. };
  29. /**
  30. * Renders the final view; executing the action and putting the resulting view script in a related layout.
  31. * @param {tart.mvc.uri.Router} router Application's router.
  32. */
  33. tart.mvc.Renderer.prototype.render = function(router) {
  34. var oldLayout = this.currentLayout,
  35. oldLayoutContext = this.currentLayoutContext,
  36. oldViewScript = this.currentViewScript,
  37. oldAction = this.currentAction,
  38. viewMarkup,
  39. layout,
  40. view = new tart.mvc.View(),
  41. controller = new (router.getController())(),
  42. action = this.currentAction = new tart.mvc.Action(router.getParams(), this.defaultLayout, view, controller);
  43. // if there is an action already executed and it has a deconstructor, call it.
  44. if (oldAction)
  45. goog.typeOf(oldAction.deconstructor) == 'function' && oldAction.deconstructor();
  46. // execute the action
  47. var actionResult = router.getAction().call(action);
  48. if (actionResult instanceof tart.mvc.uri.Redirection) {
  49. return;
  50. }
  51. // if there is a view script already rendered and it has a deconstructor, call it.
  52. if (oldViewScript)
  53. goog.typeOf(oldViewScript.deconstructor) == 'function' && oldViewScript.deconstructor();
  54. this.currentViewScript = action.view;
  55. // generate the view markup
  56. viewMarkup = action.getViewScript().call(action.view);
  57. if (viewMarkup instanceof tart.mvc.uri.Redirection) {
  58. return;
  59. }
  60. // instantiate the layout, set its content and then markup.
  61. layout = new tart.mvc.Layout(action.view);
  62. layout.setContent(viewMarkup);
  63. this.currentLayout = action.getLayout();
  64. // have to reset the layout if the action's layout is different than the previous one
  65. if (this.currentLayout != oldLayout) {
  66. layout.resetLayout = true;
  67. // if there is a layout already rendered and it has a deconstructor, call it.
  68. if (oldLayoutContext)
  69. goog.typeOf(oldLayout.deconstructor) == 'function' && oldLayout.deconstructor.call(oldLayoutContext);
  70. this.currentLayout.call(layout);
  71. }
  72. layout.render(this.dom_);
  73. // call respective render callback functions; if there are any. These let the developers
  74. // watch out for rendering events.
  75. goog.typeOf(action.view.onRender) == 'function' && action.view.onRender();
  76. if (this.currentLayout.onViewRender)
  77. goog.typeOf(this.currentLayout.onViewRender) == 'function' && this.currentLayout.onViewRender.call(layout);
  78. this.currentLayoutContext = layout;
  79. };