I've come up with a clever bit of Javascript that allows one to add
'before' and 'after' hooks to any method of an object. I've named it
actsAsDecorator in the spirit of all the acts_as_* plugins for
ActiveRecord, and I've published it on my blog at:
http://beppu.lbox.org/articles/2006/08/22/the-decorator-pattern-for-javascript
.
OO purists will note that's it's not a true implementation of the GoF
Decorator pattern, but it has similar functionality (and I think it's
a lot more straight-forward to do in Javascript, because you can
change things around at runtime instead of having to wrap everything
in classes.)
I also find that it works particularly well with Ajax.InPlaceEditor,
because it lets me add hooks to various methods and lets me implement
the functionality that I need without having to bug the
script.aculo.us maintainers about adding yet another parameter to
Ajax.InPlaceEditor's options. I find it to be really useful, and I
think some of you out there will like it, too, so check it out.
Colin
Exactly. You can add flexibility where it did not exist before with
actsAsDecorator.
(...and I'm glad that prototype and scriptaculous provide as many
hooks as they do, but there's no way the maintainers could anticipate
every possible hook someone will need. However, with
actsAsDecorator(), they won't have to anymore, because the power to
add hooks is in your hands now.)
Isn't this kind of like aspect oriented programming? I definitely like the decorator stuff, John, keep up the good work.