Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong> - I've added an example of using RequireJS with modular HTML components. Build tool example included - <a href="https://github.com/simonsmith/modular-html-requirejs" rel="noreferrer">https://github.com/simonsmith/modular-html-requirejs</a></p> <p>I have also written a blog article about this - <a href="http://simonsmith.io/modular-html-components-with-requirejs/" rel="noreferrer">http://simonsmith.io/modular-html-components-with-requirejs/</a></p> <hr> <p>The approach of just using <code>main.js</code> for everything is <a href="https://twitter.com/jrburke/status/191266657526747137" rel="noreferrer">probably more suited to a single page application</a>.</p> <p>The way I've handled this situation is to only include common site-wide JS in the <code>main.js</code> file:</p> <p><strong>On every page:</strong></p> <pre><code>&lt;script src="require.js" data-main="main"&gt;&lt;/script&gt; </code></pre> <p><strong>main.js</strong></p> <pre><code>require.config({ // config options }); require(['jquery', 'common/ajaxLoader', 'common/someOtherModule'], function($, ajax, otherModule) { // Modules that do stuff on every page are instantiated here }); </code></pre> <p><strong>page1.html</strong></p> <pre><code>&lt;script&gt; require(['scripts/page1']); &lt;/script&gt; </code></pre> <p><strong>page1.js</strong></p> <pre><code>require(['jquery', 'page1Module'], function($, module){ // page1 specific stuff here }); </code></pre> <p>The above example is just one of a couple of ways to handle it. Note <a href="http://requirejs.org/docs/api.html#jsfiles" rel="noreferrer">the difference between loading a plain JavaScript file and a module</a>.</p> <p>A rule of thumb I follow is to keep all reusable modules (or Classes if it makes it easier to conceptualise) inside a <code>define</code> with their own dependencies etc and then use <code>require</code> to grab those modules, use their methods or interact with them in some way. </p> <p>Using this pattern will almost certainly require use of the domReady module <a href="http://requirejs.org/docs/download.html#domReady" rel="noreferrer">that is a separate plugin for RequireJS</a>. Use this <a href="https://twitter.com/jrburke/status/205332720589619201" rel="noreferrer">instead of a ready function in jQuery</a> for example, as it allows modules to begin downloading before the DOM is ready which reduces the wait for your code to execute.</p> <p><strong>Edit</strong> You may wish to <a href="https://github.com/requirejs/example-multipage" rel="noreferrer">see another example of multi-page application</a> in the RequireJS repo</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. 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