Phill
08-10-2006, 11:23 AM
Hi,
Firstly, it's my first post on here so -- hello!
Secondly, I did try to do a search, I couldn't find anything so I apologise if it's been asked a million times before - please feel free to point me in the right direction :)
Thirdly - my question:
I'm creating a remote scripting (ajax) based address book. I'm trying to setup an 'onload' event handler, which points to a class method.
The code is (I'm using prototype, by the way):
var AddressBook = Class.create();
AddressBook.prototype = {
initialize: function() {
// do initialisation stuff here
},
load_addresses: function() {
// make ajax call...
},
load_addresses_cb: function(results) {
// callback function
}
};
var ab = new AddressBook();
The problem comes when I try to setup load_addresses to be the event handler, for example:
window.addEventListener('load', ab.load_addresses, false);
What happens here is that the function is getting executed, but any references to "this" seem to refer to the window rather than the object it's part of.
So, my question is, is there any way of getting "this" to work properly when you're passing functions around? Sorry if this isn't very clear, I'll post with clarification if necessary :)
Thanks!
- Phill
Firstly, it's my first post on here so -- hello!
Secondly, I did try to do a search, I couldn't find anything so I apologise if it's been asked a million times before - please feel free to point me in the right direction :)
Thirdly - my question:
I'm creating a remote scripting (ajax) based address book. I'm trying to setup an 'onload' event handler, which points to a class method.
The code is (I'm using prototype, by the way):
var AddressBook = Class.create();
AddressBook.prototype = {
initialize: function() {
// do initialisation stuff here
},
load_addresses: function() {
// make ajax call...
},
load_addresses_cb: function(results) {
// callback function
}
};
var ab = new AddressBook();
The problem comes when I try to setup load_addresses to be the event handler, for example:
window.addEventListener('load', ab.load_addresses, false);
What happens here is that the function is getting executed, but any references to "this" seem to refer to the window rather than the object it's part of.
So, my question is, is there any way of getting "this" to work properly when you're passing functions around? Sorry if this isn't very clear, I'll post with clarification if necessary :)
Thanks!
- Phill