I'm having a problem with a circular reference in my marionette.js application..
The problem is:
App.js creates the router with an controller, and that controller requires the app.js again so it can add the views to the regions.. As you can see below (the controller) when i print the Application
it returns undefined because of the circulare reference..
controller.js:
define(
['app', 'views/ProjectItemView'],
function (Application, ProjectItemView)
{
'use strict';
console.log(Application); // undefined
return Marionette.Controller.extend({
showProjects : function()
{
Application.main.show(new ProjectItemView());
console.log('project');
}
});
}
);
router.js:
define
(
['marionette'],
function(Marionette)
{
'use strict';
return Marionette.AppRouter.extend
(
{
appRoutes:
{
'dashboard/projects' : 'showProjects'
}
}
);
}
);
app.js
define
(
[
'backbone', 'marionette', 'jquery', 'routers/DashboardRouter', 'controllers/DashboardController',
'views/dashboard/Header'
],
function(Backbone, Marionette, $, DashboardRouter, DashboardController, Header)
{
'use strict';
var app = new Marionette.Application();
app.addRegions({
header: '#header',
main: '#main',
});
app.header.show(new Header());
app.on('initialize:after', function () {
Backbone.history.start({pushState: true});
app.router = new DashboardRouter({
controller : DashboardController
});
});
$(document).on("click", "a[href^='/']", function(event)
{
var href = $(event.currentTarget).attr('href');
console.log(href);
event.preventDefault();
app.router.navigate(href, {trigger: true});
return false;
});
return app;
}
);
Now what is the best way to solve this problem? I'm totally new to this so i don't really know what the best practice is..
Also is it correct that I add the views in my controller? Or should i do that somewhere else?
Thanks in advance