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
You should be able to asynchronously load your app
and any of the sub-apps
. If the sub-apps
require the main app
, they should list it as a dependency. RequireJS will resolve the dependencies for you.
Only thing you should watch out for are circular dependencies, but that shouldn't be anything you can't fix.
Listing the routers
of your sub-apps
in your main.js
sounds right to me.
subApps
could need some models
I decided to load it in app.js
. Is it right this way?A module should list any and all other modules it uses as its dependencies. Therefore, if app.js
and subapp1/subapp1.js
both require models/user.js
, they should both list models/user.js
as one of their dependencies.
This way, if you change any of the modules in such a way that it no longer requires models/user.js
you can remove the dependency from that particular module without risk of missing dependencies in another module.
Besides being the savest way of managing your dependencies, it's also nice and clear for other developers or even yourself when looking at your code in the future.
app.addInitializer
.app.userModel
will be fetched when I perform app.header.show
. Should it be ok?Fetching data should be initiated manually. While showing a view in a Region
does trigger the View
to be rendered, it doesn't trigger the view's Model
or Collection
to fetch data.
What you can do, is to let your view
listen to the events on your Model
or Collection
and re-render the view once the Model
or Collection
has successfully fetched new data.
Marionette's CollectionView
automatically binds some events to add
, change
and remove
events of it's Collection
in orde to automatically manage it's ItemViews
if the Collection
changes in any way.
main.js
the subapp1/subapp1.router
instead of subapp1/subapp1
?As I already stated in question 2, it's good to load your routers in main.js
.
One reason to do this, is that you could decide not to load all your sub-apps
up front, but instead only load them once they're actually needed. The moment such a sub-app
's routes get triggered is as good a time as any to load that sub-app
's core module and possibly some of it's dependencies, but that would of-course require the sub-app
's Router
to be activated before the sub-app
is loaded.