Search code examples
javascriptbackbone.jsmarionette

Marionette Controller Best Practice


According to the v2.4.1 Marionette documentation controllers are being deprecated:

Warning: deprecated. The Controller object is deprecated. Instead of using the Controller class with the AppRouter, you should specify your callbacks on a plain Javascript object.

I'm confused as to what the best practices are now that they are deprecated? Does this mean that the AppRouter is also being deprecated? If so what are the current patterns being used to develop large scale Marionette applications?


Solution

  • You can probably just use Marionette.Object. It's basically the same as the Controller.

    To use a plain JavaScript object with the AppRouter you could do something like this:

        var MyController = Marionette.Object.extend({/*...*/});
        var AnotherController = Marionette.Object.extend({/*...*/});
    
        var myController = new MyController();
        var anotherController = new AnotherController();
    
        var plainJsObject = {
          doStuff: myController.doStuff,
          doSomethingDifferent: anotherController.doSomethingDifferent
        };
    
        var router = Marionette.AppRouter.extend({
          controller: plainJsObject
        });