[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');
Comments
- Stephen Tramer 2011-04-15
Worse than expected. The 'this' symbol does not play nice in a lot of situations.
- Stephen Tramer 2011-04-15
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.
- Justin 2011-04-15
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?
- Stephen Tramer 2011-04-15
I think this will do what you want (let me know if it doesn't):
a.addEventListener('action', function(e) { // Do stuff Ti.App.removeEventListener('action', e.source); }
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).
- Justin 2011-04-15
That works. Thanks Stephen!
- Justin 2011-04-15
Whoops, I take that back. It doesn't seem to work.
- Stephen Tramer 2011-04-15
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.
- Justin 2011-04-15
The event listener appeared not to be removed.
- Stephen Tramer 2011-04-15
Thanks for the information. I've created #1043 and will address the issue as quickly as I can.
- Justin 2011-04-15
Thanks Stephen
- Stephen Tramer 2011-04-15
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.
- Justin 2011-04-15
Currently using that, thanks Stephen.
- Stephen Tramer 2011-04-15
See #1043. Also, functions in event listeners do not belong to an object; they're callbacks. Marking this invalid.
- Lee Morris 2017-03-02 Closing as invalid.