slinky2000
slinky2000

Reputation: 2673

In requireJS, requesting global scripts more than one?

I'm making a requireJS/backbone/jquery app and I'm wondering do I need to require in those global libraries in every script?

// App View

define(
[
    'jquery',
    'underscore',
    'backbone'
],
function($, _, Backbone) {
    var App = Backbone.View.extend( /* code here */ )
    return App;
});

// and then later in my application:
// Router

define(
[
    'jquery',
    'underscore',
    'backbone'
],
function($, _, Backbone) {
    var Router = Backbone.Router.extend( /* code here */ )
    return Router;
});

etc.

Or once they are loaded in my global app view can I forget about them? Can I just:

// App View

define(
[
    'jquery',
    'underscore',
    'backbone'
],
function($, _, Backbone) {
    var App = Backbone.View.extend( /* code here */ )
    return App;
});

// and then later in my application:
// Router

define(
[],
function() {
    var Router = Backbone.Router.extend( /* code here */ )
    return Router;
});

Upvotes: 0

Views: 51

Answers (1)

Rob W
Rob W

Reputation: 349132

Always declare the dependencies. It's bad practice to rely on global variables, and defeats the point of using RequireJS for module management.

Upvotes: 4

Related Questions