Reputation: 13430
I decided to divide my web app
in different Apps
(app1, app2).
Why from js/app1.js
I get undefined
?
How should I access the mainApp from the app1 using requirejs?
Here's my code:
js/main.js
require([
'js/app',
'js/app1/app1',
'js/app2/app2'
],
function (App)
{
"use strict";
App.initialize();
});
js/mainApp.js
define([''],
function(app)
{
var mainApp = new Backbone.Marionette.Application();
mainApp.start();
mainApp.initialize = function() {
Backbone.history.start();
}
return mainApp;
});
js/app1/app1.js
define(['js/mainApp],
function(app)
{
console.log(app); // undefined
});
Upvotes: 1
Views: 2012
Reputation: 3040
With require.js, if there is an error in one of the required file, sometimes it doesn't show as an error but the return value becomes undefined. In your case, app
in js/app1/app1.js
is undefined most likely due to an error in js/mainApp.js
.
Just from the code you provided, js/mainApp.js
has a define
statement with an empty string, but expects App
to be passed to it's function. Try removing the empty string and just pass an empty list ([]
) to define
, and not pass App
to the function there.
If your actual code doesn't have these syntax problems but you're still getting undefined
, please update the code snippets in your question.
Try this:
js/mainApp.js
define(['jquery', 'underscore', 'backbone', 'backbone.marionette'],
function($, _, Backbone)
{
var mainApp = new Backbone.Marionette.Application();
// Make sure main app is not undefined here.
console.log('mainApp:', mainApp);
mainApp.start();
mainApp.initialize = function() {
Backbone.history.start();
};
return mainApp;
});
Don't forget to provide the correct path for the JS libraries.
Upvotes: 1