javascript - RequireJs define default deps -


usually requirejs module looks like:

define('modulename', ['jquery', 'underscore', 'backbone'], function($, _, backbone){ }); 

because every file in setup requires underscore , backbone have them automaticly available in module without having define them dependencies.

so like:

define('modulename', ['jquery'], function($){ $("div.someclass").addclass('hide'); // works var model = backbone.model.extend(); // works }); 

is possible?

if yes how or keyword have for?

the modules you're interested in have attached outer scope. default, backbone, underscore, jquery etc remain attached global scope unless call noconflict() on them (not modules provide nicety). attaching modules global scope isn't great solution, accomplish you're asking , default behavior anyway. better alternative define outer module (or require() call) contains dependencies in addition named sub-modules. otherwise, of reason using requirejs lost.

edited example:

require(['underscore', 'backbone'], function (_, backbone) { define('modulename', ['jquery'], function($){ $("div.someclass").addclass('hide'); // works var model = backbone.model.extend(); // works }); //other modules here, otherwise pointless } ); 

even useful if you're defining multiple named modules in same file. best solution perspective unfortunately explicitly import modules , allow implicit module naming, above example closest you're asking for. implicit module names, the requirejs api docs:

you can explicitly name modules yourself, makes modules less portable -- if move file directory need change name.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -