this inside addeventlistener callback
The 'this' inside an addeventlistener callback is always null and I think this is not a nice thing.
Such way I can't do pass as callback an custom object method ad referencing that object inside the method..
for example I can't do
customObject = {
var1: 1,
callbackmetod: function(e){alert(this.var1)}
}
titaniumObject.addEventListener('click', customObject.callbackmetod)
1 Answer
-
Accepted Answer
You can store your object within the object that is being clicked and then access it within the click event itself. Here's an example.
var customObject = { var1: 1, callbackmetod: function(e){ alert(e.source.myobject); } } titaniumObject.myobject = customObject; titaniumObject.addEventListener('click', customObject.callbackmetod);
BTW - e.source will be titaniumObject in this example because it is the source of the event and objects in Titanium can be assigned custom properties safely.