actsAsDecorator( ) -- works great with Ajax.InPlaceEditor

1 view
Skip to first unread message

John Beppu

unread,
Aug 27, 2006, 2:07:14 AM8/27/06
to rubyonrail...@googlegroups.com
Hi prototype and scriptaculous fans.

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 Mollenhour

unread,
Aug 28, 2006, 12:32:37 AM8/28/06
to rubyonrail...@googlegroups.com
This is really cool, thanks for the post. I extended the InPlaceEditor
before with a popup calendar but maybe this would have been easier. I
extend other things occasionally as well and imagine this will come in
handy because the onxxx options that most sau and prototype stuff has
isn't always flexible enough. And then there are non-prototypish things
like calendars that have even less flexibility :)

Colin

John Beppu

unread,
Aug 29, 2006, 2:27:10 AM8/29/06
to rubyonrail...@googlegroups.com
On 8/27/06, Colin Mollenhour <elite...@mollenhour.com> wrote:
> imagine this will come in
> handy because the onxxx options that most sau and prototype stuff has
> isn't always flexible enough. And then there are non-prototypish things
> like calendars that have even less flexibility :)
>
> 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.)

Ryan Gahl

unread,
Aug 29, 2006, 9:52:23 AM8/29/06
to rubyonrail...@googlegroups.com
Isn't this kind of like aspect oriented programming? I definitely like the decorator stuff, John, keep up the good work.

Matt Spendlove

unread,
Aug 29, 2006, 11:34:15 AM8/29/06
to rubyonrail...@googlegroups.com
Sure is.

I've got an AOP library that I can I'll try and publish for feedback when I get a minute !

Basic idea is :

Aspects.addAround(Logger.prototype.AOPLogger, this, /rating/);

It'll match all methods on the current object with named like "rating" and apply an around advice. In this case the advice simple entry / exit logs the method calls dumping args and returns etc...

Matt

Ryan Gahl wrote:

John Beppu

unread,
Aug 29, 2006, 12:57:44 PM8/29/06
to rubyonrail...@googlegroups.com
On 8/29/06, Ryan Gahl <ryan...@gmail.com> wrote:
Isn't this kind of like aspect oriented programming? I definitely like the decorator stuff, John, keep up the good work.


Yeah, that's what people are telling me.  I wasn't really familiar with AOP before this, and I'm still not that familiar with it now.  Perhaps I'm looking at the wrong sites, but the literature for AOP that I've found is kinda heavy on theory and not that easy to understand.

What's funny is that I might have accidentally brought AOP to the masses.  (...or at least a watered down version of AOP (that's still useful))






Reply all
Reply to author
Forward
0 new messages