I'm trying to include the App instance to use it's event aggregator as shown here
I get an error when I include the instance in a view.
Kicking things off in the Requirejs config file, from App.Bootloader.js:
require(['App'], function (App){
App.start();
});
from App.js:
define(function (require){
//...requisite includes $, _, Backbone, Marionette ...
var Layout = require('Layout');
var App = new Marionette.Application();
App.addRegions({
main: '#view_content'
});
App.addInitializer(function (){
App.main.show(new Layout());
//... adding router etc ...
Backbone.Marionette.TemplateCache.loadTemplate = function (template, callback){
callback.call(this, Handlebars.compile(template));
};
Backbone.history.start();
});
return App;
});
From Layout.js:
define(function(require){
var View = require('folder/folder/View');
//template contains #sub div
var template = require('text!template.html');
return Marionette.Layout.extend({
template: template,
regions: {
sub: '#sub'
},
initialize: function(){
//wait till template is rendered in dom
_.defer(function(region){
region.sub.show(new View());
}, this)
}
});
});
From /folder/folder/View.js:
define(function (require){
//...requisite includes $, _, Backbone, Marionette ...
var App = require('App');
return Marionette.ItemView.extend({});
});
Where I get the error "'Error: Module name 'App' has not been loaded yet for context: _"
Any ideas? Lemme know if you need more information.
I'm also looking for a good way to handle with this kind of situations using marionette and require.js
I have worked on a solution but I don't know if it is the best way, here are my thoughts:
This is a possible solution:
app.js
view.js
it is important to remember that require is asynchronous, so when we use it like this it will not be executed immediately:
we can prepare objects wich point to the external context, like this:
user1248256 is correct. I had the same problem. My App needed a controller and my Controller needed App.
By passing in the controller (view for your code) as part of options I don't have to add it to the require.js definition.
Now in App I don't have to "require" the PublicController
Hope that helps.
Andrew
I generally think it's bad practice to use the app's EventAggregator when using requireJS, if for no other reason than it's really easy to wind up with a circular reference.
Just define a separate EventAggregator module that can be required by App, View, and Layout, then add it to the dependencies of any module that needs it.
I guess you have a problem with circular dependencies.
App
needsView
andView
needsApp
. Hmm… But whyView
requiresApp
? I can't figure it from your code. In the end, are you sureView
needsApp
? By the way, I think you mistyped. The firstFrom /folder/folder/View.js
probably should beFrom Layout.js
.