Source: mvc/MobileRenderer.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.MobileRenderer');
  15. goog.require('tart.mvc.MobileAction');
  16. goog.require('tart.mvc.Renderer');
  17. /**
  18. * @constructor
  19. * @extends {tart.mvc.Renderer}
  20. * @param {tart.mvc.LayoutTemplate} layout The default layout of the application. This layout will be used when an
  21. * action chooses not to set its own layout.
  22. * @param {Element} dom DOM element this renderer will render the application in.
  23. */
  24. tart.mvc.MobileRenderer = function(layout, dom) {
  25. goog.base(this, layout, dom);
  26. };
  27. goog.inherits(tart.mvc.MobileRenderer, tart.mvc.Renderer);
  28. /**
  29. * Renders the final view; executing the action and putting the resulting view script in a related layout.
  30. * @param {tart.mvc.uri.Router} router Application's router.
  31. */
  32. tart.mvc.MobileRenderer.prototype.render = function(router) {
  33. var oldLayout = this.currentLayout,
  34. oldLayoutContext = this.currentLayoutContext,
  35. oldViewScript = this.currentViewScript,
  36. oldAction = this.currentAction,
  37. viewMarkup,
  38. layout,
  39. view = new tart.mvc.View(),
  40. controller = new (router.getController())(),
  41. action = this.currentAction = new tart.mvc.MobileAction(router.getParams(), this.defaultLayout, view,
  42. 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. if (this.currentLayout && this.currentLayout.beforeViewRender)
  47. goog.typeOf(this.currentLayout.beforeViewRender) == 'function' &&
  48. this.currentLayout.beforeViewRender.call(layout);
  49. // execute the action
  50. var actionResult = router.getAction().call(action);
  51. if (actionResult instanceof tart.mvc.uri.Redirection) {
  52. return;
  53. }
  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. var isSameView = oldAction && oldAction.getViewScript() == action.getViewScript();
  73. // if there is an action already executed and it has a deconstructor, call it.
  74. if ((!isSameView || (isSameView && action.refresh != false)) && oldViewScript)
  75. goog.typeOf(oldViewScript.deconstructor) == 'function' && oldViewScript.deconstructor();
  76. if (!isSameView || (isSameView && action.refresh != false) || !oldViewScript)
  77. layout.render(this.dom_);
  78. // call respective render callback functions; if there are any. These let the developers
  79. // watch out for rendering events.
  80. goog.typeOf(action.view.onRender) == 'function' && action.view.onRender();
  81. if (this.currentLayout.onViewRender)
  82. goog.typeOf(this.currentLayout.onViewRender) == 'function' && this.currentLayout.onViewRender.call(layout);
  83. this.currentLayoutContext = layout;
  84. };