I organised the file structure of my web app, which is using RequireJs and Backbone.Marionette,in this way:
|- main.js
|- app.js
|- /subapp1
|- subapp1.js
|- subapp1.router.js
|- /subapp2
|- subapp2.js
|- subapp2.router.js
|- /colections
|- /views
To loads the modules I use requireJs.
Here's my code, for each module I put some questions.
// main.js
define([
'app',
'subapp1/subapp1.router',
'subapp2/subapp2.router'
], function (app) {
"use strict";
app.start();
});
Questions:
1) Is right to load asynchronously the app and subapps even if subapps need app?
2) for the subApps is right to load the router which needs the app?
// app.js
/*global define*/
define([
'backbone',
'marionette',
'models/user'
], function (Backbone, Marionette, UserModel) {
"use strict";
var App = new Marionette.Application();
App.addRegions({
header: '#header',
sidebar: '#sidebar',
mainColumn: '#main-column',
rightColumn: '#right-column'
});
App.on("initialize:before", function () {
this.userModel = new UserModel();
this.userModel.fetch();
});
App.on("initialize:after", function () {
Backbone.history.start();
});
return App;
});
Questions:
3) Since the subApps
could need some models
I decided to load it in app.js
. Is it right this way?
// subapp1/subapp1.js
/*global define*/
define([
'app',
'subapp1/views/sidebarView',
'subapp1/views/headerView'
], function (app, SidebarView, HeaderView) {
"use strict";
app.addInitializer(function(){
app.header.show(new HeaderView({userModel: app.userModel}));
app.sidebar.show(new SidebarView({userModel: app.userModel}));
});
});
Questions:
4) about this module I am not sure about the app.addInitializer
.
I am not sure for example if the app.userModel
will be fetched when I perform app.header.show
.
Should it be ok?
// subapp1/subapp1.router.js
/*global define*/
define([
'marionette',
'tasks/app'
], function (Marionette, app) {
"use strict";
var Router = Marionette.AppRouter.extend({
appRoutes: {
'tasks': 'tasks',
'tasks/:id': 'taskDetail',
'*defaults': 'tasks'
}
});
return new Router({
controller: app
});
});
Question:
5) is it ok to load from the main.js
the subapp1/subapp1.router
instead of subapp1/subapp1
?