You need to bind each method that you use in addEvent so that the this
variable is properly referenced. Here's how I would have coded this
(untested of course):
var TestEventObj = new Class({
initialize: function(element) {
this.element=$(element);
this.attach();
},
testEvent: function(event){
console.log(event.page.x);
},
attach: function(){
//This line is necessary because you need the reference returned by
bindWithEvent in order to detach the listener later.
this.boundTestEvent = this.testEvent.bindWithEvent
(this);
this.element.addEvent
('mousemove',this.boundTestEvent);
this.element.addEvent('click',this.detach.bind(this));
},
detach: function(){
this.element.removeEvent
('mousemove',this.boundTestEvent);
}
});
Give that a test. Hopefully it'll work.
Jon
On Jul 6, 3:52 pm, rpflo <[email protected]> wrote:
> Got the event working, but I can't figure out how to detach.
>
> var TestEventObj = new Class({
>
> initialize: function(element) {
> this.element=$(element);
> this.attach();
> },
>
> testEvent: function(event){
> console.log(event.page.x);
> },
>
> attach: function(){
> this.element.addEvent('mousemove',this.testEvent);
> this.element.addEvent('click',this.detach);
> },
>
> detach: function(){
> this.element.removeEvent('mousemove',this.testEvent);
> }
>
> });
>
> In detach() I get :: TypeError: Result of expression
> 'this.element' [undefined] is not an object.
>
> On Jul 6, 4:11 pm, Ryan Florence <[email protected]> wrote:
>
> > So I've got this stuff in a class
>
> > startMove: function(el){
> > el.addEvent('mousemove',this.move(el,event));
> > },
>
> > move: function(el,event){
> > el.setStyles({
> > 'left': event.page.x-(el.getSize().x/2),
> > 'top': event.page.y-(el.getSize().y/2)
> > });
> > },
>
> > stop: function(el){
> > el.removeEvent('mousemove',this.move);
> > }
>
> > But it doesn't work ... Result of expression 'event' [undefined] is
> > not an object.
>
> > I know there's a syntax thing going on here. How do I send that event
> > object into the move function?
>
> > I had this working when the whole move function's code was nested
> > inside the startMove function. But then I couldn't remove the event.
>
> > Thanks!
>
> > Ryan Florencehttp://ryanflorence.com/blog/