New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to trigger events and corresponding method #265
Conversation
…d of object passed in to it's constructor
…l view trigger events to use triggerMethod.
I like it! I'm a big fan of scripted conventions, they are the only ones I will not break on a foolish day. The only thing to consider is to check first if the function onFoo is defined before onFoo gets called. THX |
I like the idea of tying events and method names together. +1 for consistency. Re: breaking the API - could you add a method alias which prints a deprecation warning to the console ?? |
@tbhodel right - i have a basic check here https://github.com/marionettejs/backbone.marionette/pull/265/files#L9R22 but i need to make that check for @andyl yeah, that can be done. |
This looks great to me, anything to consolidate like logic. On Tue, Oct 2, 2012 at 9:36 AM, Derick Bailey notifications@github.comwrote:
|
Add method to trigger events and corresponding method
Being a huge fan of consistency I'm loving this change. I am curious about two things:
As an aside, the naming convention for before and after events, 'beforeAction' / 'actioned', strikes me unbalanced; 'beforeAction' / 'afterAction' seems like it would have been a little cleaner. |
@ccamarat can you open a new issue ticket w/ exactly that same text? I'd like to track that as a separate but related issue. thanks. |
Done. Sorry for the delay. |
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 totrigger
inside of every Marionette view so that it now callstriggerMethod
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" functiontriggerMethod("before:close")
fires the "onBeforeClose" functionAll 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 callonFoo: 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.