Is there a reason for not using Marionette.Controller with a Marionette.AppRouter?

499 views
Skip to first unread message

Naresh Bhatia

unread,
Mar 1, 2014, 10:04:21 PM3/1/14
to backbone-...@googlegroups.com
The documentation for Marionette.Controller says that it is "a multi-purpose object to use as a controller for modules and routers". However, I see that in many cases people do not use the Marionette.Controller with their routers. For example, David Sulc uses a plain old object called API in his book:

    ContactsAppRouter.Router = Marionette.AppRouter.extend({
      appRoutes: {
        "contacts(/filter/criterion::criterion)": "listContacts",
        "contacts/:id": "showContact",
        "contacts/:id/edit": "editContact"
      }
    });

    var API = {
      listContacts: function(criterion){
        require(["apps/contacts/list/list_controller"], function(ListController){
          executeAction(ListController.listContacts, criterion);
        });
      },

      ...
    };

    ContactManager.addInitializer(function(){
      new ContactsAppRouter.Router({
        controller: API
      });
    });

Is this purely a matter of taste or is there a specific advantage to this pattern?

A followup question: What is a good use case for using a Marionette Controller over a pure JavaScript object? I have not yet found a compeling example.

Dmytro Yarmak

unread,
Mar 2, 2014, 3:22:42 AM3/2/14
to backbone-...@googlegroups.com
Main advantages of Marionette.Controller over Plain Old JavaScript Object is:
1. It has `initialize` method - we can do some initialization in it or pass dependency to controller
2. It has `close` (`onClose`) - we can do some finalization work when we don't need controller anymore
3. It extends Backbone.Events - so we can use `this.listenTo` and `this.stopListening` in controller.

So in my opinion it is better to use Marionette.Controller when there is some initialization process in controller and/or controller need to store some state.
Also it is better when we pass dependencies to controller. In this case code will be more testable.
Reply all
Reply to author
Forward
0 new messages