Monday, October 8, 2007

Metaprogramming in the wild - script.aculo.us effect's afterUpdate only the first time

So, I want to fade in some content and I need to adjust a scroll on the content.

I can't adjust the scroll while it's hidden because none of the scroll info will be set.

I can't update it after the animation, because there will be a nasty jump.

What I really want is to adjust the scroll after the first frame of animation - after the content has been shown but while it's still mostly transparent.

The only script.aculo.us hook that will fire at this point is afterUpdate, but it's also going to fire on every animation.

Enter some metaprogramming. Define a afterUpdate that cleans up after itself:


new Effect.Appear( this.details_div, {duration:.5,
afterUpdate : function(){
FooManager.adjustScroll();
this.afterUpdate = undefined;
}
});


This works like a champ.