简体   繁体   中英

Confused about RequireJS dependency

I am trying to wrap my head around dependencies in requirejs.

  1. If I already declared dependencies for a file using shim , do I need to re-declare it when I define the module in that file?
  2. If I use require to load dependencies such as backbone, do I need to re-declare it when I define a module that is loaded as part of require ?

Here's my code so far:

require.config({
    //alias
    paths: {
        Backbone: 'libs/backbone-min',
        Config: 'config',
        Dom: 'dom',
        App: 'app'
    },

    //dependencies
    shim: {
        'Backbone': ['libs/underscore-min'],
        'Dom': ['libs/sizzle']
    }
});

//used to load and use stuff
require(['Config','Dom','App','Backbone'], function(){

});

So in dom.js can I just define a module using define(function(){...}); and start using Sizzle? Or do I still need to define it like this define(['libs/sizzle'], function(){...});

Also if I define a module in app.js, do I still need to load backbone in define , since I already included it as part of require() .

1) If I already declared dependencies for a file using shim, do I need to re-declare it when I define the module in that file?

For every module you need to define it's set of dependencies.

2) If I use require to load dependencies such as backbone, do I need to re-declare it when I define a module that is loaded as part of require?

If you want to use backbone as dependency in arbitary modyle you could write

define(['backbone'], function(Backbone) { .. }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM