1

I have an app which consist of several modules, eg: contacts, meetings, reminders.

The urls are probably

1. /#contacts       ->    contacts module, listing page
2. /#contacts/45    ->    contacts module, detail page of contact id 45

3. /#meeting        ->    meetings module, listing page
4. /#meeting/67     ->    meetings module, detail page of meeting id 67

I use Marionette App.module('ContactMgr') , App.module('MeetingMgr') to separate modules.

Question: Which is the best practice?


A) separating routers in each module

 // in file js/app/ContactMgr/Router.js
 App.ContactMgr.Router = Marionette.AppRouter.extend({
    appRoutes: {
     'contacts':     'listContacts',
     'contacts/:id': 'showContact'
    }
 });

 //////////////////////////////////////////////

 // in file js/app/MeetingMgr/Router.js
 App.MeetingMgr.Router = Marionette.AppRouter.extend({
    appRoutes: {
     'meetings':     'listMeetings',
     'meetings/:id': 'showMeeting'
    }
 });

B) one router in app

 // in file js/app/app.js
 App.Router = Marionette.AppRouter.extend({
    appRoutes: {
     'contacts':     'listContacts',
     'contacts/:id': 'showContact',
     'meetings':     'listMeetings',
     'meetings/:id': 'showMeeting'
    }
 });
Community
  • 1
  • 1
Ksthawma
  • 1,257
  • 1
  • 16
  • 28

1 Answers1

0

As for me, multiple routers solution is better because each router makes sense only for it's module. Each router should use it's own controller and controller should use views/collections/models from it's module.

Actually the major idea of module is that you can add or remove them with minor changes in other parts of your application. So in case you use multiple routers solution if you want to add/remove module in your app you'll just need to add/remove it's instantiation in your app and routers controllers will be in your module.

But if you use single router solution you'll also have to edit you router, controller, etc.

So multiple routers approach is more modular.

  • A concern with the multi- solution: when users are seeing the meeting module, the contact module may need to stop, to conserve memory. After stopping the contact module, I think now there is no way to go to the contact module? because no routers look at route 'contacts'. – Ksthawma Dec 23 '14 at 08:24
  • I don't think there'll be difference in memory usage. You marionette modules are usually consist of routers/controllers/views/models. Views and models are created only when needed, so there will be no instantiated 'contact' module's views/models when you're in 'meeting' module. The only things that always exist are routers and controllers. They're usually created on application initialization and exist during application lifetime. So if all modules are loaded only routers and controllers are instantiated and there's no significant difference between one huge router/controller or many small. – Stanislau Tarazevich Dec 24 '14 at 08:58
  • If you have some complicated logic in your modules which uses much memory, has many event handlers, etc then maybe stopping and restarting modules make sense and single router will work better. But if you have something close to default CRUD then stopping modules most likely won't bring you much performance – Stanislau Tarazevich Dec 24 '14 at 09:03