/* appjet:version 0.1 */ /* appjet:library */ // Copyright (c) 2009, Herbert Vojčík // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php) /** * Sets model for the page. * Optional. There is no default model. */ page.setModel = function(model) { this.model = model; }; /** * Sets view-model for the page. * This view-model is set for the page view as well. * Optional. Default view-model is true. */ page.setViewModel = function(viewModel) { this.viewModel = viewModel; if ("view" in this) { this.view.model = viewModel; } }; /** * Sets the view for the page. Sets the view model from the page view-model. * Optional. Default view is empty DIV(). */ page.setView = function(view) { this.view = view; if ("viewModel" in this) { view.model = this.viewModel; } }; /** * Sets the presenter for the page. It must be a function. It will be called with page model as a parameter. * Optional. Default presenter is legacy dispatch(). */ page.setPresenter = function(presenter) { if (typeof presenter !== "function") { throw new Error("A presenter must be a function"); } this.presenter = presenter; }; page.setViewModel(true); page.setView(DIV()); page.setPresenter(dispatch); // Repository for redirected printx functions. var _rawprint = DIV(); function _addRawPrintSectionTo(result) { if (_rawprint.length) { result.push( DIV({style:"clear:both"}), DIV({style:"border-top: 1px solid #ccc; margin-top: 1.2em; margin-bottom: 1.2em"}, SMALL(STRONG("Raw print:"))), _rawprint ); } } /** * Sets up MVP printing mode, then runs the page presenter with the page model as an argument. * You should call it in place you normally call dispatch(). */ function mvp() { var oldBodyWrite = page.body.write; var oldRender = page.render; page.render = function () { this.render = oldRender; this.body.write = oldBodyWrite; return this.render(); }; page.body.write({ toString: function() { var result = [page.view]; _addRawPrintSectionTo(result); return result.map(toHTML).join(''); }}); page.body.write = function(rawText) { _rawprint.push(html(rawText)); }; page.presenter(page.model); } /* appjet:server */ import("lib-app/mvp"); /**@ignore*/ var view = DIV(); view.push(P("Ahoy!")); page.setView(P("This is fake view that will be replaced")); page.setViewModel(""); print("Raw text before mvp call<>\n*", BR()); printp("A paragraph"); page.setView(view); /**@ignore*/ function get_main() { var list = OL(); view.push(list); print(CODE(appjet._native.codeSection("server")[1].code)); list.push(LI("This is the real contents of the page.")); list.push(LI("Print-style functions are redirected to raw print section (useful as ad-hoc debug output). ", "The raw print section is only shown when there actually was some raw print."), LI("The contents of the page is created by setView with view object. ", "You can manipulate this view object after setView.")); list.push(LI("This way, object/functional style is not mixed with print-style, ", "what just caused bad style and confusion.")); list.push(LI("You can setViewModel for the page, which is propagated to view, ", "as well as setPresenter if default dispatch() style is not enough.")); list.push(LI("The view model is: ", view.model)); //page.setView("
"); // This should appear as visible text, not as html. } mvp(); print("Raw text after mvp call<>\n*", BR());