marionettejs/backbone.marionette — pull request #265 by derickbailey (2012-10-02 14:00:17+00:00)
Title: Add method to trigger events and corresponding method

Another bit I've been thinking about for a while, and finally put together... I want to get some feedback on this to see if it's really valuable or not.

Many of the events in Marionette's views have corresponding methods. For example, "render" event has "onRender" method. But many of the corresponding methods are named inconsistently: "before:close" before has "beforeClose" method ... note the missing "on" in front of that.

To clean up the inconsistencies and to facilitate additional "onWhatever" methods that are called when events are triggered, I added a `Marionette.triggerMethod` function and attached it to the base View. I also updated all calls to `trigger` inside of every Marionette view so that it now calls `triggerMethod` instead.

This has a small effect of cleaning up some code, but a larger effect of making all triggered event + method combinations more consistent, and making the corresponding method available for all triggered events, not just the ones we had coded explicitly, previously.

A few bits on how this works:

When an event is triggered, the first letter of each section of the event name is capitalized, and the word "on" is tagged on to the front of it. Examples:

* `triggerMethod("render")` fires the "onRender" function
* `triggerMethod("before:close")` fires the "onBeforeClose" function

All arguments that are passed to the `triggerMethod` call are passed along to both the event and the method, with the exception of the event name not being passed to the corresponding method.

* `triggerMethod("foo", bar)` will call `onFoo: function(bar){...})`

I'd like some feedback on this idea and implementation. It does break some existing function names, such as "beforeClose" which is now "onBeforeClose". But I think the small breaking change is worth the consistency change. There may be some additional updates for methods like "onShow" or others that might need to be examined / changed as well.