Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would be interested in knowing other's view on this, but what I did so organize and execute the correct set of JS code for each page, is to have a kind of routing engine as the basis of my js app where I add a 'controller' list that each section adds it self to and the routing engine calls the controller.init() method based on the url.</p> <pre><code>myApp = {}; (function(app, $) { var defaultController = "Default"; var routes = []; routes["Default"] = defaultController; var currentControllerFromUrl = function() { // match urls in the form ../mvc/controllerName.mvc/... var re = new RegExp(/\/(\w*)/i); var m = re.exec(document.location); if (m == null) { return null; } else { return m[1]; } }; var currentActionFromUrl = function() { // match urls in the form ../mvc/controllerName.mvc/action/ var re = new RegExp(/\/mvc\/\w*.mvc\/(\w*)(\/|\b)/i); var m = re.exec(document.location); if (m == null) { return null; } else { return m[1].toLowerCase(); } }; var currentWebPageFromUrl = function() { var re = new RegExp(/\/(\w*).html/i); var m = re.exec(document.location); if (m == null) { return null; } else { return m[1]; } }; var populateControllerRoutes = function(app) { for (var ctl in app.controllers) { routes[ctl] = ctl; } }; var theApp = { controllers: {}, run: function() { this.initController(); }, initController: function() { var urlController = currentWebPageFromUrl(); // populate routes populateControllerRoutes(this); if (urlController &amp;&amp; routes[urlController]) { // load the correct controller into the activeController this.controller = new this.controllers[routes[urlController]](); this.controller.init(); var action = currentActionFromUrl() || "index"; if (action &amp;&amp; this.controller[action + "Action"] !== undefined) this.controller[action + "Action"](); } } }; window.myApp = $.extend(app, theApp); })(myApp || {}, jQuery); // and somewhere else (function($) { myApp.controllers.default = function() { this.init = function() { }; this.indexAction = function() { // index action init $("#res").text("hi there!"); } } })(jQuery); $(function() { myApp.run(); }); </code></pre> <p>With this approach, the controller functions don't need to be in one file, so during development you can keep your controller functions in separate files nicely organized and as part of the build/deploy, combine them and minimize them all into one JS file. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload