Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-786] crash using 'this' in removeEventListener

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2011-04-15T02:36:25.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.4.0
ComponentsiOS
Labelsn/a
ReporterNolan Wright
AssigneeJeff Haynie
Created2011-04-15T02:36:22.000+0000
Updated2017-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

  1. Stephen Tramer 2011-04-15

    Worse than expected. The 'this' symbol does not play nice in a lot of situations.

  2. 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.

  3. 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?

  4. 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).

  5. Justin 2011-04-15

    That works. Thanks Stephen!

  6. Justin 2011-04-15

    Whoops, I take that back. It doesn't seem to work.

  7. 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.

  8. Justin 2011-04-15

    The event listener appeared not to be removed.

  9. Stephen Tramer 2011-04-15

    Thanks for the information. I've created #1043 and will address the issue as quickly as I can.

  10. Justin 2011-04-15

    Thanks Stephen

  11. 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.

  12. Justin 2011-04-15

    Currently using that, thanks Stephen.

  13. Stephen Tramer 2011-04-15

    See #1043. Also, functions in event listeners do not belong to an object; they're callbacks. Marking this invalid.

  14. Lee Morris 2017-03-02

    Closing as invalid.

JSON Source