javascript - Difference between local and global require in RequireJS -


using requirejs, don't believe understand difference between following 2 different usages of 'require'. in case, talking browser, not node.js. 1 of questions is: can require dependencies synchronously on front-end using requirejs?

first there this:

    define(function(){           //below require global require of requirejs, aliased requirejs.           require(['module'],function(mod){             //mod loaded here             }        }); 

then there this:

   define(['require'],function(require){        require(['dep'],function(dep){  //require local not global            //dep loaded here          }       //now require subsequently called 'local' require      var dep = require('dependency'); //i doubt possible on front-end..or it?       }); 

i guess don't see purpose of using require argument define on front-end, there one?

my confusion arising because of example here:

http://requirejs.org/docs/api.html

relative module names inside define(): require("./relative/name") calls can happen inside define() function call, sure ask "require" dependency, relative name resolved correctly:

define(["require", "./relative/name"], function(require) {     var mod = require("./relative/name"); }); 

from above, can guess can make synchronous calls other modules requirejs on front-end if 1 module relative another?

likewise, example:

generate urls relative module: may need generate url relative module. so, ask "require" dependency , use require.tourl() generate url:

define(["require"], function(require) {     var cssurl = require.tourl("./style.css"); }); 

so, question is: difference between local require , global require? doesn't appear local require only/primarily commonjs modules.

all of these examples use asynchronous module loading, not synchronous. code looking @ bit confusing because seems synchronous, in reality not.

in example:

define(["require", "./relative/name"], function(require) {     var mod = require("./relative/name"); }); 

the asynchrony explicit. because module id './relative/name' in dependency array, function not called until has been (asynchronously) loaded. require call return module has (asynchronously) loaded.

in supported syntax, asynchrony hidden:

define(function(require) {     var mod = require("./relative/name"); }); 

but asynchronous. requirejs rewrites code contain dependency array 1 above. asynchrony get, despite appearances.

why useful , different? because can challenging match dozen members of array corresponding places in function turned variables. consider example docs:

define([ "require", "jquery", "blade/object", "blade/fn", "rdapi",          "oauth", "blade/jig", "blade/url", "dispatch", "accounts",          "storage", "services", "widgets/accountpanel", "widgets/tabbutton",          "widgets/addaccount", "less", "ostheme", "jquery-ui-1.8.7.min",          "jquery.textoverflow"], function (require,   $,        object,         fn,         rdapi,           oauth,   jig,         url,         dispatch,   accounts,           storage,   services,   accountpanel,           tabbutton,           addaccount,           less,   ostheme) {  }); 

another reason useful , differnt local require allows resolve relative module paths. lets define bunch of modules relative each other without explicitly specifying directory in, handy if directory might change.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -