Hi,
I''m fairly new to Prototype and am experiencing a slight problem in
getting an event observed. I''ve had a quick browse through the list
and can''t find an example of the exact issue I''m having here.
Basically I have a class, and as part of the initialization of the
class it adds an event listener to an array of links around thumbnail
images. I want it to call a function from within the class, passing
the event and the thumbnail image object, I am using the following
line of code to achieve this:
Event.observe(thumbnaillink, ''click'',
this.alertImage.bindAsEventListener(this,
thumbnaillink.firstDescendant()));
This just doesn''t work - it doesn''t create a firebug error
(and I
don''t know where else I could look for errors). Interestingly if I
explicitly write the function in line it does work i.e:
Event.observe(thumbnail, ''click'', function(e){
var clickedimage = $A(arguments)[1];
alert(''Clicked the following image '' +
clickedimage.readAttribute(''src''));
}.bindAsEventListener(this, thumbnaillink.firstDescendant()));
But I didn''t really want to write the code in this fashion (seems less
tidy, and, of course, I will be wanting to do much more with the
clicked image than display an alert with the scr attribute).
Here is the full class for context, any help (on any of the techniques
used, not necessarily just this problem) will be much appreciated:
var ImageViewer = Class.create({
initialize: function(){
this.mainimage = $(''mainimage'').firstDescendant();
this.thumbnaildiv = $(''thumbnails'').firstDescendant();
this.thumbnaillinks = $(this.thumbnaildiv).childElements();
this.thumbnaillinks.each(function(thumbnaillink) {
// The following 4 lines work!
//Event.observe(thumbnail, ''click'', function(e){
// var clickedimage = $A(arguments)[1];
// alert(''Clicked the following image '' +
clickedimage.readAttribute(''src''));
//}.bindAsEventListener(this, thumbnaillink.firstDescendant()));
Event.observe(thumbnaillink, ''click'',
this.alertImage.bindAsEventListener(this,
thumbnaillink.firstDescendant()));
thumbnaillink.removeAttribute(''href'');
} );
},
/**
* Called when a thumbnail is clicked
*/
alertImage: function(e){
var clickedimage = $A(arguments)[1];
alert(''Clicked the following image '' +
clickedimage.readAttribute(''src''));
}
});
Event.observe(window, ''load'', function() {
try {
if ($(''mainimage'')) {
new ImageViewer();
}
}
catch(Error) {}
});
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---