mvp.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* appjet:version 0.1 */
  2. /* appjet:library */
  3. // Copyright (c) 2009, Herbert Vojčík
  4. // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
  5. /**
  6. * Sets model for the page.
  7. * Optional. There is no default model.
  8. */
  9. page.setModel = function(model) {
  10. this.model = model;
  11. };
  12. /**
  13. * Sets view-model for the page.
  14. * This view-model is set for the page view as well.
  15. * Optional. Default view-model is true.
  16. */
  17. page.setViewModel = function(viewModel) {
  18. this.viewModel = viewModel;
  19. if ("view" in this) { this.view.model = viewModel; }
  20. };
  21. /**
  22. * Sets the view for the page. Sets the view model from the page view-model.
  23. * Optional. Default view is empty DIV().
  24. */
  25. page.setView = function(view) {
  26. this.view = view;
  27. if ("viewModel" in this) { view.model = this.viewModel; }
  28. };
  29. /**
  30. * Sets the presenter for the page. It must be a function. It will be called with page model as a parameter.
  31. * Optional. Default presenter is legacy dispatch().
  32. */
  33. page.setPresenter = function(presenter) {
  34. if (typeof presenter !== "function") {
  35. throw new Error("A presenter must be a function");
  36. }
  37. this.presenter = presenter;
  38. };
  39. page.setViewModel(true);
  40. page.setView(DIV());
  41. page.setPresenter(dispatch);
  42. // Repository for redirected printx functions.
  43. var _rawprint = DIV();
  44. function _addRawPrintSectionTo(result) {
  45. if (_rawprint.length) {
  46. result.push(
  47. DIV({style:"clear:both"}),
  48. DIV({style:"border-top: 1px solid #ccc; margin-top: 1.2em; margin-bottom: 1.2em"},
  49. SMALL(STRONG("Raw print:"))),
  50. _rawprint
  51. );
  52. }
  53. }
  54. /**
  55. * Sets up MVP printing mode, then runs the page presenter with the page model as an argument.
  56. * You should call it in place you normally call dispatch().
  57. */
  58. function mvp() {
  59. var oldBodyWrite = page.body.write;
  60. var oldRender = page.render;
  61. page.render = function () {
  62. this.render = oldRender;
  63. this.body.write = oldBodyWrite;
  64. return this.render();
  65. };
  66. page.body.write({ toString: function() {
  67. var result = [page.view];
  68. _addRawPrintSectionTo(result);
  69. return result.map(toHTML).join('');
  70. }});
  71. page.body.write = function(rawText) {
  72. _rawprint.push(html(rawText));
  73. };
  74. page.presenter(page.model);
  75. }
  76. /* appjet:server */
  77. import("lib-app/mvp");
  78. /**@ignore*/
  79. var view = DIV();
  80. view.push(P("Ahoy!"));
  81. page.setView(P("This is fake view that will be replaced"));
  82. page.setViewModel("<model>");
  83. print("Raw text before mvp call<>\n*", BR());
  84. printp("A paragraph");
  85. page.setView(view);
  86. /**@ignore*/
  87. function get_main() {
  88. var list = OL();
  89. view.push(list);
  90. print(CODE(appjet._native.codeSection("server")[1].code));
  91. list.push(LI("This is the real contents of the page."));
  92. list.push(LI("Print-style functions are redirected to raw print section (useful as ad-hoc debug output). ",
  93. "The raw print section is only shown when there actually was some raw print."),
  94. LI("The contents of the page is created by setView with view object. ",
  95. "You can manipulate this view object after setView."));
  96. list.push(LI("This way, object/functional style is not mixed with print-style, ",
  97. "what just caused bad style and confusion."));
  98. list.push(LI("You can setViewModel for the page, which is propagated to view, ",
  99. "as well as setPresenter if default dispatch() style is not enough."));
  100. list.push(LI("The view model is: ", view.model));
  101. //page.setView("<div>&ndash;</div>"); // This should appear as visible text, not as html.
  102. }
  103. mvp();
  104. print("Raw text after mvp call<>\n*", BR());