arturadib / agility

Javascript MVC for the "write less, do more" programmer
http://agilityjs.com
MIT License
542 stars 70 forks source link

Added pre: and post: hooks to custom agility controller events #46

Closed tristanls closed 13 years ago

tristanls commented 13 years ago

If an agility object has a custom agility event. One can bind to a pre: or post: hook. For example:

var obj = $$({}, '<div/>', {
  'myEvent': function() {
    var l = $$({label: 'myEventLabel'}, '<span data-bind="label"/>');
    this.append(l);
  }
});
obj.bind('post:myEvent', function() {
  var l = $$({label: 'postHookEvent'}, '<span data-bind="label"/>');
  obj.append(l);
});
obj.bind('pre:myEvent', function() {
  var l = $$({label: 'preHookEvent'}, '<span data-bind="label"/>');
  obj.append(l);
});
arturadib commented 13 years ago

Hey Tristan, thanks so much for the contribution and sorry for taking so long to respond.

I noticed that I haven't documented the use of _ for events: when a controller is preceded by an underscore, it will be fired before the bare (without underscore) controller. For example:

var obj = $$({}, {}, {
  '_test': function(){ alert('1'); },
  'test': function(){ alert('2'); }
});
obj.trigger('test');

should alert '1' and then '2'.

Does this solve your problem? Or perhaps you actually need to make multiple bindings per event? If so, I'd love to hear more about your specific application just so that I have a better appreciation for the feature.

Thanks!

tristanls commented 13 years ago

Hey Artur,

Good to hear from you again.

The first reason I didn't want to touch the _ events because they seemed "private" to me, used by the plumbing of the framework. But we actually do have a need for multiple bindings per event, specifically, two.

First, is the generic "hook" concept. We're keeping our widgets open to extension and closed to modification ( at least abstractly ). At first glance, the post: hook seems an equivalent of the ~ extended controller. The difference, being that widgets composed of widgets would extend the controller events as ~ while other helpers / workers / apis could hook into them using pre: and post:. It's the sequence of execution and the assumptions of what should happen. In my mind, pre: and post: are external to the widget, while ~ is internal. The odd one out here is how _ trumps the pre: hook, but this came from my assumption to treat it as "framework private".

Second, is a layer on top of the first one above, and that is analytics. And herein lies our need for multiple bindings per event. Once we have a main application tested and ready to go, it can then be extended with custom code via hooks into the main application ( first layer of bindings ), then it seems to me a clean way to add analytics to pretty much anything we want by using multiple pre: and post: bindings ( second layer of bindings ).

Because agility objects are so easy to use, we are using them in a more general sense for non-displayable parts of the webpage as helpers, or api interfaces, etc. where some of these hooks and analytics requirements are more apparent.

Are they needed for straightforward webpages? Probably not. Are they needed for heavy weight client side applications? I think so.

arturadib commented 13 years ago

That sounds good, Tristan. Thanks for taking your time to explain it.

Generally, I'm reluctant to implement new features as I'm afraid of bloating the framework:

http://gettingreal.37signals.com/ch05_Start_With_No.php

So I think these pull request discussions are a great place to question whether the feature is broad and useful enough to be part of the framework.

Please keep up the awesome work!