Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple js files in page, each starting with require(): bad practice or OK?
    text
    copied!<p>I am writing a framework that allows your modules to define which JS files will be added to the page before serving it. The idea is that a module might add something like this: </p> <pre><code>require([ 'dojo/topic', 'dojo/_base/window', 'hotplate/hotDojoWidgets/AlertBar' ], function( topic, win, AlertBar ){ globalAlertBar = new AlertBar( { background: '#FF4444' } ); globalAlertBar.placeAt( document.body, 'first' ); globalAlertBar.startup(); topic.subscribe('globalAlert', function( message, displayFor ){ displayFor = displayFor || 2500; globalAlertBar.set( 'message', message ); globalAlertBar.show( displayFor ); }); }); </code></pre> <p>This allows me for example to allow other widgets around the application to publish <code>globalAlert</code> in order to show an alertBar. Right now: </p> <ul> <li>Any JS/Dojo file added to the list of JS to be loaded starts with "require". To me, this is simply asynchronous code that will run at some point after loading. Most of the code loaded this way in the framework will wait for topics to be published, and will act accordingly </li> <li>The "application" will simply be one of those modules, which will subscribe to a topic publishing the container's object </li> <li>"Proper" Dojo widgets loaded by require() will obviously use "define" -- that's business as usual, no worries there </li> </ul> <p>Questions: </p> <p>1) If I run <code>ready()</code> within one of the JS/Dojo files referenced in the page, will that be a page-level ready(), including all of the .js files loaded in the document? Or only for the files loaded by the current file? </p> <p>2) I realise that the "right" way of doing things is by having only one JS file that runs <code>require()</code>, and have everything else loaded from that one (or even better using data-dojo-main ). However, I am trying to define modules (with server and client code) which work independently and add themselves to the page... So, is it actually bad practice, having having a bunch of files in the application page, and then having them <code>require()</code>'ing and running Dojo code? </p> <p>Thank you! </p> <p>Merc. </p> <p>P.S. If you are curious, <a href="https://github.com/mercmobily/hotplate" rel="nofollow">https://github.com/mercmobily/hotplate</a> ... the project started after a post here!</p>
 

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