franco
franco

Reputation: 141

Require.js require context

Suppose that I need test variable when I'm inside the require statement. Which is the best approach, if exists, in order to access the variable that I need? Is there a way to change context?

    somefunction: function(id) {
        var test ="xxx";
        require(['models/Model', 'views/View'], function(ArticleModel, ArticleView) {
            var collection = new Collection();
// I need to access the TEST variable HERE
        });
    }

Upvotes: 0

Views: 1735

Answers (1)

Adam Easterling
Adam Easterling

Reputation: 2405

In the example you included, you will be able to use the 'test' variable, as it was defined in a scope that encompasses the call to require().

Here's another StackOverflow answer that explains this in more detail -- How do JavaScript closures work?

Upvotes: 1

Related Questions