marionettejs/backbone.marionette — pull request #321 by derickbailey (2012-10-24 17:59:08+00:00)
Title: Controller

Related to #280

I write code like this, a lot (copied from the Marionette JSFiddle playground - http://jsfiddle.net/derickbailey/M5J8Q/ )

```js
App.module("SampleModule", function(Mod, App, Backbone, Marionette, $, _){

// Define a view to show
// ---------------------

var MainView = Marionette.ItemView.extend({
template: "#sample-template"
});

// Define a controller to run this module
// --------------------------------------

var Controller = function(region){
this.region = region;
};

_.extend(Controller.prototype, {
show: function(){
var model = new Backbone.Model({
contentPlacement: "here"
});

var view = new MainView({
model: model
});

this.region.show(view);
}

});

// Initialize this module when the app starts
// ------------------------------------------

Mod.addInitializer(function(){
Mod.controller = new Controller(App.mainRegion);
Mod.controller.show();
});
});
```

I'd like to reduce this code repetition by providing a very basic `Controller` type in Marionette that acts as a module level controller, facilitating workflow ( http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/ ), and acting as a mediator ( http://addyosmani.com/largescalejavascript/#mediatorpattern ) between sub-modules and other collaborators.

This implementation includes basic abilities like passing an options parameter to the controller constructor, triggering events, a built-in event binder, and supporting an initialize method:

```js
MyController = Marionette.Controller.extend({

initialize: function(options){
this.whatever = options.whatever;
},

someMethod: function(){
// ...
}

});
```

I haven't written docs yet, but would do that before merging, if we think this is a good idea.

I'd love to get some feedback on this: good idea? bad idea? needs work?