arkitrave log

arkitrave :: log

4/25/2006

Passing parameters into your AddEvent call

Filed under:

If, like me, you’re still using some variant of an addEventListener function to add DOM events, you may run into the need to pass parameters into the function that you’re calling. (Just for the record, I use Angus Turnbull’s Add Event Manager, as it lets me use the this keyword in the functions it calls).

My event listeners are set up like this (my addEvent script is in a helper object called EventHelper):

EventHelper.AddEvent(window, 'load', foo, false);

Let’s say you’re using prototype-based JavaScript. In this case, this should refer to the “class” your function is a part of. As soon as you write any event handler call, the scope of this changes to the event itself (click, mouseover, etc) for that line of code.

Let’s say we need to pass parameters to foo in the line of code above. In addition to whatever parameters the function needs, we probably need to pass three additional items: the element the handler is attached to, the correct object scope of this and the event reference itself.

The code looks like this:

var thisReference = this;
EventHelper.AddEvent(elementReference, 'click',
pointer = function(e) {thisReference.foo(this,
thisReference, other, parameters, e); }, false);

thisReference stores the correct this scope to a variable - this is the scope of the prototype object. We’ll lose it otherwise when we write the AddEvent line. elementReference, obviously, is the element that the event handler is being attached to. e is the event, which needs to be passed into the pointer function if it’s going to be available to pass into foo(). The fun part is the pointer function. We are setting up an anonymous function which has the click event as its parameter. This pointer function is only used to call the actual function (foo) you want attached to the event. You can use it to pass whatever parameters you want to foo; just make sure you pass this, thisReference and e if you need the element, the prototype object and the event in the function you’re calling.

The pointer function gets the actual, original event (e). When the event happens and pointer is executed, it calls foo, passing it your parameters.

Enjoy.

Tags:,

4/21/2006

Where Was I?

Filed under:

Thanks to Carl for tagging me on a meme which asks what I was doing one, five, and ten years ago.

One year ago:

One year ago my daughter was not yet four months old. My wife had gone back to work, but only 20 hours a week; my daily routine consisted of four hours playing with Naia in the morning and freelancing in the afternoon. All in all, a pretty good schedule, but one that couldn’t last forever. I began my current job in June. Projected career path: Front-end web developer.

Five years ago:

Autumn and I had been married for about eight months. We were living in a shoebox “garden level” studio apartment in an historic 1878 townhouse in the Grove Place neighborhood of Rochester, New York. Tiny, but “plenny-a-chahm”, as the east-coast realtors like to say. Autumn was finishing her Master of Music at Eastman School of Music, and I was working temp job after temp job trying to pay the rent and support the Rochester restaurant economy. Actually, by April Autumn had a job as well, and we were socking away money to buy our house in Buffalo. I was just about to start a four-year stint in architecture school. Projected career path: Architect.

Ten years ago:

Ah, 1996. The year I graduated from high school. I was a music geek, excited to get away from home (2000 miles away!) and start my undergrad in piano performance at the University of Puget Sound. I was busy being a spring-semester senior, doing as little work as possible to graduate without totally messing up my GPA and cutting a few classes here and there, mainly just so I could say that I cut a few classes in high school. I worked part-time at a fantastic Italian cafe, where I developed a taste for great food that my income has always had a hard time supporting. I was also busy with rehearsals for my part as Arvide in our school’s production of Guys and Dolls. Projected career path: Musician/college music professor.

That was a fun nostalgia trip. I’ll pass the meme-torch on to Zach, Mike, and (because he has been tagged already but still hasn’t written anything) Erik.