views:

224

answers:

4

In jquery an event hadler's binding is the event generating DOM element (this points to the dom element). In prototype to change the binding of an event handler one can use the bindAsEventListener function; How can I access both the instance and the DOM element from a event handler?
Similar to http://stackoverflow.com/questions/117361/how-can-i-bind-an-event-handler-to-an-instance-in-jquery

function Car(){
    this.km = 0;
    $("#sprint").click(this.drive); //setup event handler
}

// event handler
// in it I need to access both the clicked element
// and the binding object (instance of car)
Car.prototype.drive = function(){
    this.km += 10; // i'd like to access the binding (but jq changes it)
    this.css({ // also the element
        left: this.km 
    }); 
    // NOTE that is inside this function I want to access them not elsewhere
}

var car = new Car();
A: 

The value this would otherwise point to (that is, the element the handler is attached to) is also passed in the currentTarget property of the event object. So, if you use the binding function you talked about:

Car.prototype.drive = function(e) {
    // this will be your car object
    // e.currentTarget will be the element that you attached the click handler to
}
Matti Virkkunen
I cannot use `bindAsEventListener` as it is from prototype.
clyfe
@clyfe: It's fairly straightforward to implement by yourself...
Matti Virkkunen
A: 

ok here you are :

var car = {km:0};
$("#sprint").click(function(){
    car.km += 10;
    $(this).css({ left: car.km });
});

I have not tested it but should be straight forward or as for where you went wrong is on your "this" which is looking at the "sprint" element not the "car" object.

Val
this is exactley what I am trying to avoid :)
clyfe
fair enough :) I was wondering why go the long way
Val
+1  A: 

Just bind a variable to this and use that.

function Car(){
    this.km = 0;
    var that = this;
    $("#sprint").click(function(){
         that.drive(this);
    });
}


Car.prototype.drive = function(element){
    this.km += 10; // i'd like to access the binding (but jq changes it)
    this.css({ // also the element
        left: this.km 
    }); 
    alert(element.innerHTML);
    // NOTE that is inside this function I want to access them not elsewhere
}

The handler passes the element to the instance

Sean Kinsey
you didn't read the whole question
clyfe
I figured that would solve it self :) But I updated the answer with how to access both the class instance and the element
Sean Kinsey
the other question also has this kind of answer but i find it pretty ugly. is this the proposed way to do this in jquery community? there must be something better ... or is just a matter of personal taste. i'll let this roast some more, and probably accept your answer.
clyfe
The listener will probably receive an `event` object containing a reference to the `element` that triggered the event, you could probably use that. But I'm not sure which one I find the ugliest..
Sean Kinsey
Yes but the issue is that I loose reference to the current object (because of the this swich) the element I can get via the event.currentTarget
clyfe
`this` is bound to the scope, and so in order for you to have `this` point to the correct scope then you will either need to have the correct object on the left side of the '.' operator, or you will have to use `.call` or `.apply` to apply the scope to the function you wish to call. The easiest thing though is to keep the correct object on the left side by introducing a scoped variable like `that`.
Sean Kinsey
A: 

Hmm, maybe you can use jQuery.proxy()?

http://api.jquery.com/jQuery.proxy/

acidtv