A few thoughts :

1) You need to bind the object to get it to work:
  onSuccess: this.switchPrompt.bind(this),
  onFailure: this.dontCheck.bind(this)
that will fake out Javascript and set up the "this" reference to work
correctly for you I think.

2) On another note, I've noticed that it's appropriate to use the
Event.observe() method to set up onclick listeners.  For instance:

Event.observe(this.chkbox,
'click',this.switchRequired.bindAsEventListener(this));

In this case it probably makes no difference, but in general it helps
you around browser inconsistencies with events and listeners.

3) Do you really want to cache the whole event? I don't know if that
will cause any weirdness because of the browser trying to destroy it or
something.  In general I would cache the source of the event only, which
is probably what you need anyway:
  this.cache = Event.element(evt);
but maybe I'm wrong and you do want the whole event and caching it is
not a problem.

Hope that helps,

rahul

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Jason
Hummel
Sent: Thursday, October 27, 2005 9:37 AM
To: [email protected]
Subject: [Rails-spinoffs] Problem passing event object

Hi all,

I'm having a problem with an Ajax.Request. I can't figure out how I
can pass an event (an onclick on a checkbox) which causes the Ajax
Request, to be recognized in an onSuccess function. Here's my
simplified code:

var RequiredField = Class.create();

RequiredField.prototype = {
  initialize: function(chkbox) {
  this.chkbox = chkbox;
  this.chkbox.onclick = this.switchRequired.bindAsEventListener(this);
},
        
switchRequired: function(evt) {
  this.evt = evt; // Set the event to a property
  alert(this.evt);
  var params = $F(this.chkbox);
  var myAjax = new Ajax.Request('server/checkRequired.html',
        {method: 'get', parameters: params, onSuccess: this.switchPrompt
,onFailure: this.dontCheck}
},
        
switchPrompt: function() {
  alert(this.evt); // Try to pick up the this.evt var, but it comes
bakc as undefined
},
        
dontCheck: function(response) {
  // failure
}
}
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to