I have a circular link problem in marionette.js app.
The problem is this:
App.js creates a router with a controller, and that controller again requires app.js so that it can add views to regions. As you can see below (controller), when I type Application , it returns undefined due to the circulare link ..
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 am completely new to this, so I donβt know what is the best practice.
Is it also true that I am adding views to my controller? Or should I do it somewhere else?
Thank you in advance