[TIMOB-786] crash using 'this' in removeEventListener
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2011-04-15T02:36:25.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 1.4.0 |
Components | iOS |
Labels | n/a |
Reporter | Nolan Wright |
Assignee | Jeff Haynie |
Created | 2011-04-15T02:36:22.000+0000 |
Updated | 2017-03-02T18:40:41.000+0000 |
Description
add this code to app.js
Ti.App.addEventListener('hello', function(){
Ti.API.info('Hello event fired!');
Ti.App.removeEventListener('hello', this);
});
Ti.App.fireEvent('hello');
Worse than expected. The 'this' symbol does not play nice in a lot of situations.
Okay, see http://www.quirksmode.org/js/this.html">http://www.quirksmode.org/js/this.html. Apparently 'this' is supposed to screw up if you don't use it when a function is bound to an object, but we should still consider it a (low-priority) issue.
I have windows with events that I would like to fire only once. I'm using the code pattern above to remove the event promptly, but it crashes at removeEventListener. Know of a workaround?
I think this will do what you want (let me know if it doesn't):
This will probably have to be the pattern you use in the future as well. 'this' support in event listeners was erroneous in 0.8 because an event listener isn't REALLY a function attached to an object (see the link in the above comments for more).
That works. Thanks Stephen!
Whoops, I take that back. It doesn't seem to work.
Can you give me any additional information about the failure, or is it just that the event listener isn't removed? I can create a bug for this if there isn't one already.
The event listener appeared not to be removed.
Thanks for the information. I've created #1043 and will address the issue as quickly as I can.
Thanks Stephen
Justin, I updated #1043 with some sample code that shows how to appropriately remove an event listener. Please let me know if it doesn't work for you in that ticket, and re-open it if necessary.
Currently using that, thanks Stephen.
See #1043. Also, functions in event listeners do not belong to an object; they're callbacks. Marking this invalid.
Closing as invalid.