javascript - Calling require(['app']) Only Once -
javascript - Calling require(['app']) Only Once -
how can phone call requirejs require(['app'], function() {}); 1 time @ origin whole application subsequent require(["..."], function(...){}); don't need wrapped within require(['app']?
this set up:
1) load require.js
<script data-main="js/app.js" src="requirejs/require.min.js"></script>
2) have app.js shims , basurl configured properly.
requirejs.config({ baseurl: "scripts/js", paths: { "jquery": "../bower_components/jquery/dist/jquery.min", "modernizr": "../bower_components/modernizr/modernizr", . . . }, shim: { "jquery.migrate": ['jquery'], . . . } });
3) dynamically load js on different pages:
// home page require(['app'], function() { require(["jquery", "foundation", "foundation.reveal"], function ($, foundation, reveal){ $(document).foundation(); }); }); // catalog page require(['app'], function() { require(["jquery", "lnav/leftnavctrl","controllers/productctrl", "controllers/tabsctrl"], function ($, navctrl, productctrl, tabsctrl){ $(function() { navctrl.initleftnav(); }); }); });
unless wrap require(['app'], function()) each time phone call require("...") load external js or amd modules, app not initialized , javascript errors. above code works it's not efficient.
is there way start requirejs app before seek loading scripts?
i tried calling @ origin right after load require.min.js:
require(["app"], function (app) { app.run(); });
but didn't work.
there no provisions in requirejs ensure specific module loaded before other module loaded, other having first module load rest. trying share first module among multiple pages cannot perform work of loading specific each page.
one way can work around load app.js
regular script
element:
<script src="requirejs/require.min.js"></script> <script src="js/app.js"></script>
then next script
element can start application without requiring app.js
:
<script> require(["jquery", "foundation", "foundation.reveal"], function ($, foundation, reveal){ $(document).foundation(); }); </script>
this how i've decided launch modules in applications i'm working on right now. true, not optimized because of network round-trip, in case of applications i'm working on, still in very heavy development, , prefer leave optimization later.
note generally don't want utilize script
load requirejs modules app.js
not real module not phone call define
, okay.
another alternative utilize building tool grunt, gulp, create or else , create 1 app.js
per page , have each page load own app.js
file. file contain configuration and first require
phone call load modules specific page.
javascript requirejs amd
Comments
Post a Comment